-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch introduces a helper crypto.hash() that computes a digest from the input at one shot. This can be 1.2-1.6x faster than the object-based createHash() for smaller inputs (<= 5MB) that are readily available (not streamed) and incur less memory overhead since no intermediate objects will be created. PR-URL: #51044 Refs: nodejs/performance#136 Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]>
- Loading branch information
1 parent
4cae1b9
commit 4c46439
Showing
9 changed files
with
272 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict'; | ||
|
||
const common = require('../common.js'); | ||
const { createHash, hash } = require('crypto'); | ||
const path = require('path'); | ||
const filepath = path.resolve(__dirname, '../../test/fixtures/snapshot/typescript.js'); | ||
const fs = require('fs'); | ||
const assert = require('assert'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
length: [1000, 100_000], | ||
method: ['md5', 'sha1', 'sha256'], | ||
type: ['string', 'buffer'], | ||
n: [100_000, 1000], | ||
}, { | ||
combinationFilter: ({ length, n }) => { | ||
return length * n <= 100_000 * 1000; | ||
}, | ||
}); | ||
|
||
function main({ length, type, method, n }) { | ||
let data = fs.readFileSync(filepath); | ||
if (type === 'string') { | ||
data = data.toString().slice(0, length); | ||
} else { | ||
data = Uint8Array.prototype.slice.call(data, 0, length); | ||
} | ||
|
||
const oneshotHash = hash ? | ||
(method, input) => hash(method, input, 'hex') : | ||
(method, input) => createHash(method).update(input).digest('hex'); | ||
const array = []; | ||
for (let i = 0; i < n; i++) { | ||
array.push(null); | ||
} | ||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
array[i] = oneshotHash(method, data); | ||
} | ||
bench.end(n); | ||
assert.strictEqual(typeof array[n - 1], 'string'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
// This tests crypto.hash() works. | ||
const common = require('../common'); | ||
|
||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
const assert = require('assert'); | ||
const crypto = require('crypto'); | ||
const fixtures = require('../common/fixtures'); | ||
const fs = require('fs'); | ||
|
||
// Test errors for invalid arguments. | ||
[undefined, null, true, 1, () => {}, {}].forEach((invalid) => { | ||
assert.throws(() => { crypto.hash(invalid, 'test'); }, { code: 'ERR_INVALID_ARG_TYPE' }); | ||
}); | ||
|
||
[undefined, null, true, 1, () => {}, {}].forEach((invalid) => { | ||
assert.throws(() => { crypto.hash('sha1', invalid); }, { code: 'ERR_INVALID_ARG_TYPE' }); | ||
}); | ||
|
||
[null, true, 1, () => {}, {}].forEach((invalid) => { | ||
assert.throws(() => { crypto.hash('sha1', 'test', invalid); }, { code: 'ERR_INVALID_ARG_TYPE' }); | ||
}); | ||
|
||
assert.throws(() => { crypto.hash('sha1', 'test', 'not an encoding'); }, { code: 'ERR_INVALID_ARG_VALUE' }); | ||
|
||
// Test that the output of crypto.hash() is the same as crypto.createHash(). | ||
const methods = crypto.getHashes(); | ||
|
||
const input = fs.readFileSync(fixtures.path('utf8_test_text.txt')); | ||
|
||
for (const method of methods) { | ||
for (const outputEncoding of ['buffer', 'hex', 'base64', undefined]) { | ||
const oldDigest = crypto.createHash(method).update(input).digest(outputEncoding || 'hex'); | ||
const digestFromBuffer = crypto.hash(method, input, outputEncoding); | ||
assert.deepStrictEqual(digestFromBuffer, oldDigest, | ||
`different result from ${method} with encoding ${outputEncoding}`); | ||
const digestFromString = crypto.hash(method, input.toString(), outputEncoding); | ||
assert.deepStrictEqual(digestFromString, oldDigest, | ||
`different result from ${method} with encoding ${outputEncoding}`); | ||
} | ||
} |