mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 07:53:06 +01:00
7b802685c4
Currently the buffer benchmarks take significantly too long to complete. This drastically reduces the overall runtime by removing obsolete checked variations and reducing the iteration count. It also improves the benchmarks by removing the deprecated `new Buffer(size)` usage and some other small improvements. PR-URL: https://github.com/nodejs/node/pull/26418 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Peter Marshall <petermarshall@chromium.org> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
100 lines
2.1 KiB
JavaScript
100 lines
2.1 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common.js');
|
|
const assert = require('assert');
|
|
const bench = common.createBenchmark(main, {
|
|
source: [
|
|
'array',
|
|
'arraybuffer',
|
|
'arraybuffer-middle',
|
|
'buffer',
|
|
'string',
|
|
'string-utf8',
|
|
'string-base64',
|
|
'object',
|
|
],
|
|
len: [100, 2048],
|
|
n: [8e5]
|
|
});
|
|
|
|
function main({ len, n, source }) {
|
|
const array = new Array(len).fill(42);
|
|
const arrayBuf = new ArrayBuffer(len);
|
|
const str = 'a'.repeat(len);
|
|
const buffer = Buffer.allocUnsafe(len);
|
|
const uint8array = new Uint8Array(len);
|
|
const obj = { length: null }; // Results in a new, empty Buffer
|
|
|
|
let i = 0;
|
|
|
|
switch (source) {
|
|
case 'array':
|
|
bench.start();
|
|
for (i = 0; i < n; i++) {
|
|
Buffer.from(array);
|
|
}
|
|
bench.end(n);
|
|
break;
|
|
case 'arraybuffer':
|
|
bench.start();
|
|
for (i = 0; i < n; i++) {
|
|
Buffer.from(arrayBuf);
|
|
}
|
|
bench.end(n);
|
|
break;
|
|
case 'arraybuffer-middle':
|
|
const offset = ~~(len / 4);
|
|
const length = ~~(len / 2);
|
|
bench.start();
|
|
for (i = 0; i < n; i++) {
|
|
Buffer.from(arrayBuf, offset, length);
|
|
}
|
|
bench.end(n);
|
|
break;
|
|
case 'buffer':
|
|
bench.start();
|
|
for (i = 0; i < n; i++) {
|
|
Buffer.from(buffer);
|
|
}
|
|
bench.end(n);
|
|
break;
|
|
case 'uint8array':
|
|
bench.start();
|
|
for (i = 0; i < n; i++) {
|
|
Buffer.from(uint8array);
|
|
}
|
|
bench.end(n);
|
|
break;
|
|
case 'string':
|
|
bench.start();
|
|
for (i = 0; i < n; i++) {
|
|
Buffer.from(str);
|
|
}
|
|
bench.end(n);
|
|
break;
|
|
case 'string-utf8':
|
|
bench.start();
|
|
for (i = 0; i < n; i++) {
|
|
Buffer.from(str, 'utf8');
|
|
}
|
|
bench.end(n);
|
|
break;
|
|
case 'string-base64':
|
|
bench.start();
|
|
for (i = 0; i < n; i++) {
|
|
Buffer.from(str, 'base64');
|
|
}
|
|
bench.end(n);
|
|
break;
|
|
case 'object':
|
|
bench.start();
|
|
for (i = 0; i < n; i++) {
|
|
Buffer.from(obj);
|
|
}
|
|
bench.end(n);
|
|
break;
|
|
default:
|
|
assert.fail('Should not get here');
|
|
}
|
|
}
|