Skip to content

Commit

Permalink
benchmark: add create-hash benchmark
Browse files Browse the repository at this point in the history
PR-URL: #51026
Refs: nodejs/performance#136
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
joyeecheung authored and RafaelGSS committed Dec 15, 2023
1 parent c45a9a3 commit b148c43
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions benchmark/crypto/create-hash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const common = require('../common.js');
const { createHash } = require('crypto');
const assert = require('assert');

const bench = common.createBenchmark(main, {
n: [1e5],
});

function main({ n }) {
const array = [];
for (let i = 0; i < n; ++i) {
array.push(null);
}
bench.start();
for (let i = 0; i < n; ++i) {
array[i] = createHash('sha1');
}
bench.end(n);
assert.strictEqual(typeof array[n - 1], 'object');
}
2 changes: 1 addition & 1 deletion lib/internal/crypto/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const kState = Symbol('kState');
const kFinalized = Symbol('kFinalized');

function Hash(algorithm, options) {
if (!(this instanceof Hash))
if (!new.target)
return new Hash(algorithm, options);
if (!(algorithm instanceof _Hash))
validateString(algorithm, 'algorithm');
Expand Down

0 comments on commit b148c43

Please sign in to comment.