From b148c43244b37ea22c25948bd424b275a0b5f550 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Tue, 5 Dec 2023 23:59:31 +0100 Subject: [PATCH] benchmark: add create-hash benchmark MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/51026 Refs: https://github.com/nodejs/performance/issues/136 Reviewed-By: Matteo Collina Reviewed-By: Michaël Zasso Reviewed-By: Vinícius Lourenço Claro Cardoso Reviewed-By: Tobias Nießen Reviewed-By: Luigi Pinca --- benchmark/crypto/create-hash.js | 22 ++++++++++++++++++++++ lib/internal/crypto/hash.js | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 benchmark/crypto/create-hash.js diff --git a/benchmark/crypto/create-hash.js b/benchmark/crypto/create-hash.js new file mode 100644 index 00000000000000..119534d5c24f83 --- /dev/null +++ b/benchmark/crypto/create-hash.js @@ -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'); +} diff --git a/lib/internal/crypto/hash.js b/lib/internal/crypto/hash.js index 57fcb63518d52d..4ed97034f612fb 100644 --- a/lib/internal/crypto/hash.js +++ b/lib/internal/crypto/hash.js @@ -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');