mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
crypto: improve randomBytes() performance
PR-URL: https://github.com/nodejs/node/pull/31519 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
1 parent
7b7e7bd185
commit
59a1981a22
3 files changed
+20
-2
No files matched your search
@@ -0,0 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common.js');
|
||||
const { randomBytes } = require('crypto');
|
||||
|
||||
const bench = common.createBenchmark(main, {
|
||||
size: [64, 1024, 8192, 512 * 1024],
|
||||
n: [1e3],
|
||||
});
|
||||
|
||||
function main({ n, size }) {
|
||||
bench.start();
|
||||
for (let i = 0; i < n; ++i)
|
||||
randomBytes(size);
|
||||
bench.end(n);
|
||||
}
|
||||
@@ -6,7 +6,7 @@ const {
|
||||
} = primordials;
|
||||
|
||||
const { AsyncWrap, Providers } = internalBinding('async_wrap');
|
||||
const { Buffer, kMaxLength } = require('buffer');
|
||||
const { kMaxLength } = require('buffer');
|
||||
const { randomBytes: _randomBytes } = internalBinding('crypto');
|
||||
const {
|
||||
ERR_INVALID_ARG_TYPE,
|
||||
@@ -15,6 +15,7 @@ const {
|
||||
} = require('internal/errors').codes;
|
||||
const { validateNumber } = require('internal/validators');
|
||||
const { isArrayBufferView } = require('internal/util/types');
|
||||
const { FastBuffer } = require('internal/buffer');
|
||||
|
||||
const kMaxUint32 = 2 ** 32 - 1;
|
||||
const kMaxPossibleLength = MathMin(kMaxLength, kMaxUint32);
|
||||
@@ -52,7 +53,7 @@ function randomBytes(size, cb) {
|
||||
if (cb !== undefined && typeof cb !== 'function')
|
||||
throw new ERR_INVALID_CALLBACK(cb);
|
||||
|
||||
const buf = Buffer.alloc(size);
|
||||
const buf = new FastBuffer(size);
|
||||
|
||||
if (!cb) return handleError(_randomBytes(buf, 0, size), buf);
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ runBenchmark('crypto',
|
||||
'len=1',
|
||||
'n=1',
|
||||
'out=buffer',
|
||||
'size=1',
|
||||
'type=buf',
|
||||
'v=crypto',
|
||||
'writes=1',
|
||||
|
||||
Reference in new issue
Block a user