benchmark: (buffers) use destructuring

PR-URL: https://github.com/nodejs/node/pull/18250
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Ruben Bridgewater 2017-12-30 03:59:57 +01:00
parent 896397b5dc
commit 8e3d7623a5
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
23 changed files with 57 additions and 108 deletions

View File

@ -6,8 +6,7 @@ const bench = common.createBenchmark(main, {
n: [32], n: [32],
}); });
function main(conf) { function main({ n }) {
const n = +conf.n;
const charsPerLine = 76; const charsPerLine = 76;
const linesCount = 8 << 16; const linesCount = 8 << 16;
const bytesCount = charsPerLine * linesCount / 4 * 3; const bytesCount = charsPerLine * linesCount / 4 * 3;

View File

@ -6,8 +6,7 @@ const bench = common.createBenchmark(main, {
n: [32], n: [32],
}); });
function main(conf) { function main({ n }) {
const n = +conf.n;
const s = 'abcd'.repeat(8 << 20); const s = 'abcd'.repeat(8 << 20);
// eslint-disable-next-line no-unescaped-regexp-dot // eslint-disable-next-line no-unescaped-regexp-dot
s.match(/./); // Flatten string. s.match(/./); // Flatten string.

View File

@ -27,9 +27,7 @@ const bench = common.createBenchmark(main, {
n: [32] n: [32]
}); });
function main(conf) { function main({ n, len }) {
const n = +conf.n;
const len = +conf.len;
const b = Buffer.allocUnsafe(len); const b = Buffer.allocUnsafe(len);
let s = ''; let s = '';
let i; let i;

View File

@ -15,11 +15,7 @@ const chars = [
'𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎𠸏𠹷𠺝𠺢' // 4 bytes '𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎𠸏𠹷𠺝𠺢' // 4 bytes
]; ];
function main(conf) { function main({ n, len, encoding }) {
const n = conf.n | 0;
const len = conf.len | 0;
const encoding = conf.encoding;
var strings = []; var strings = [];
var results; var results;
if (encoding === 'buffer') { if (encoding === 'buffer') {

View File

@ -7,10 +7,8 @@ const bench = common.createBenchmark(main, {
millions: [1] millions: [1]
}); });
function main(conf) { function main({ millions, size, args }) {
const iter = (conf.millions >>> 0) * 1e6; const iter = millions * 1e6;
const size = (conf.size >>> 0);
const args = (conf.args >>> 0);
const b0 = Buffer.alloc(size, 'a'); const b0 = Buffer.alloc(size, 'a');
const b1 = Buffer.alloc(size, 'a'); const b1 = Buffer.alloc(size, 'a');
const b0Len = b0.length; const b0Len = b0.length;

View File

@ -23,13 +23,11 @@ function compareUsingOffset(b0, b1, len, iter) {
bench.end(iter / 1e6); bench.end(iter / 1e6);
} }
function main(conf) { function main({ millions, size, method }) {
const iter = (conf.millions >>> 0) * 1e6; const iter = millions * 1e6;
const size = (conf.size >>> 0); const fn = method === 'slice' ? compareUsingSlice : compareUsingOffset;
const method = fn(Buffer.alloc(size, 'a'),
conf.method === 'slice' ? compareUsingSlice : compareUsingOffset; Buffer.alloc(size, 'b'),
method(Buffer.alloc(size, 'a'), size >> 1,
Buffer.alloc(size, 'b'), iter);
size >> 1,
iter);
} }

View File

@ -27,9 +27,8 @@ const bench = common.createBenchmark(main, {
millions: [1] millions: [1]
}); });
function main(conf) { function main({ millions, size }) {
const iter = (conf.millions >>> 0) * 1e6; const iter = millions * 1e6;
const size = (conf.size >>> 0);
const b0 = Buffer.alloc(size, 'a'); const b0 = Buffer.alloc(size, 'a');
const b1 = Buffer.alloc(size, 'a'); const b1 = Buffer.alloc(size, 'a');

View File

@ -8,15 +8,11 @@ const bench = common.createBenchmark(main, {
n: [1024] n: [1024]
}); });
function main(conf) { function main({ n, pieces, pieceSize, withTotalLength }) {
const n = +conf.n;
const size = +conf.pieceSize;
const pieces = +conf.pieces;
const list = new Array(pieces); const list = new Array(pieces);
list.fill(Buffer.allocUnsafe(size)); list.fill(Buffer.allocUnsafe(pieceSize));
const totalLength = conf.withTotalLength ? pieces * size : undefined; const totalLength = withTotalLength ? pieces * pieceSize : undefined;
bench.start(); bench.start();
for (var i = 0; i < n * 1024; i++) { for (var i = 0; i < n * 1024; i++) {

View File

@ -15,10 +15,8 @@ const bench = common.createBenchmark(main, {
n: [1024] n: [1024]
}); });
function main(conf) { function main({ len, n, type }) {
const len = +conf.len; switch (type) {
const n = +conf.n;
switch (conf.type) {
case '': case '':
case 'fast-alloc': case 'fast-alloc':
bench.start(); bench.start();

View File

@ -18,10 +18,7 @@ const bench = common.createBenchmark(main, {
n: [2048] n: [2048]
}); });
function main(conf) { function main({ len, n, source }) {
const len = +conf.len;
const n = +conf.n;
const array = new Array(len).fill(42); const array = new Array(len).fill(42);
const arrayBuf = new ArrayBuffer(len); const arrayBuf = new ArrayBuffer(len);
const str = 'a'.repeat(len); const str = 'a'.repeat(len);
@ -31,7 +28,7 @@ function main(conf) {
var i; var i;
switch (conf.source) { switch (source) {
case 'array': case 'array':
bench.start(); bench.start();
for (i = 0; i < n * 1024; i++) { for (i = 0; i < n * 1024; i++) {

View File

@ -7,9 +7,7 @@ const bench = common.createBenchmark(main, {
n: [1e7] n: [1e7]
}); });
function main(conf) { function main({ len, n }) {
const len = conf.len | 0;
const n = conf.n | 0;
const buf = Buffer.alloc(len); const buf = Buffer.alloc(len);
for (let i = 0; i < buf.length; i++) for (let i = 0; i < buf.length; i++)

View File

@ -8,16 +8,14 @@ const bench = common.createBenchmark(main, {
n: [1e7] n: [1e7]
}); });
function main(conf) { function main({ n, value }) {
const n = +conf.n;
const search = +conf.value;
const aliceBuffer = fs.readFileSync( const aliceBuffer = fs.readFileSync(
path.resolve(__dirname, '../fixtures/alice.html') path.resolve(__dirname, '../fixtures/alice.html')
); );
bench.start(); bench.start();
for (var i = 0; i < n; i++) { for (var i = 0; i < n; i++) {
aliceBuffer.indexOf(search, 0, undefined); aliceBuffer.indexOf(value, 0, undefined);
} }
bench.end(n); bench.end(n);
} }

View File

@ -25,16 +25,13 @@ const bench = common.createBenchmark(main, {
search: searchStrings, search: searchStrings,
encoding: ['undefined', 'utf8', 'ucs2', 'binary'], encoding: ['undefined', 'utf8', 'ucs2', 'binary'],
type: ['buffer', 'string'], type: ['buffer', 'string'],
iter: [1] iter: [100000]
}); });
function main(conf) { function main({ iter, search, encoding, type }) {
const iter = (conf.iter) * 100000;
var aliceBuffer = fs.readFileSync( var aliceBuffer = fs.readFileSync(
path.resolve(__dirname, '../fixtures/alice.html') path.resolve(__dirname, '../fixtures/alice.html')
); );
var search = conf.search;
var encoding = conf.encoding;
if (encoding === 'undefined') { if (encoding === 'undefined') {
encoding = undefined; encoding = undefined;
@ -44,7 +41,7 @@ function main(conf) {
aliceBuffer = Buffer.from(aliceBuffer.toString(), encoding); aliceBuffer = Buffer.from(aliceBuffer.toString(), encoding);
} }
if (conf.type === 'buffer') { if (type === 'buffer') {
search = Buffer.from(Buffer.from(search).toString(), encoding); search = Buffer.from(Buffer.from(search).toString(), encoding);
} }

View File

@ -16,14 +16,11 @@ const methods = {
'iterator': benchIterator 'iterator': benchIterator
}; };
function main(conf) { function main({ size, type, method, n }) {
const len = +conf.size; const clazz = type === 'fast' ? Buffer : SlowBuffer;
const clazz = conf.type === 'fast' ? Buffer : SlowBuffer; const buffer = new clazz(size);
const buffer = new clazz(len);
buffer.fill(0); buffer.fill(0);
methods[method || 'for'](buffer, n);
const method = conf.method || 'for';
methods[method](buffer, conf.n);
} }

View File

@ -25,13 +25,12 @@ const bench = common.createBenchmark(main, {
millions: [1] millions: [1]
}); });
function main(conf) { function main({ noAssert, millions, buf, type }) {
const noAssert = conf.noAssert === 'true'; noAssert = noAssert === 'true';
const len = +conf.millions * 1e6; const len = millions * 1e6;
const clazz = conf.buf === 'fast' ? Buffer : require('buffer').SlowBuffer; const clazz = buf === 'fast' ? Buffer : require('buffer').SlowBuffer;
const buff = new clazz(8); const buff = new clazz(8);
const type = conf.type || 'UInt8'; const fn = `read${type || 'UInt8'}`;
const fn = `read${type}`;
buff.writeDoubleLE(0, 0, noAssert); buff.writeDoubleLE(0, 0, noAssert);
const testFunction = new Function('buff', ` const testFunction = new Function('buff', `

View File

@ -10,9 +10,8 @@ const bench = common.createBenchmark(main, {
const buf = Buffer.allocUnsafe(1024); const buf = Buffer.allocUnsafe(1024);
const slowBuf = new SlowBuffer(1024); const slowBuf = new SlowBuffer(1024);
function main(conf) { function main({ n, type }) {
const n = +conf.n; const b = type === 'fast' ? buf : slowBuf;
const b = conf.type === 'fast' ? buf : slowBuf;
bench.start(); bench.start();
for (var i = 0; i < n * 1024; i++) { for (var i = 0; i < n * 1024; i++) {
b.slice(10, 256); b.slice(10, 256);

View File

@ -72,13 +72,9 @@ function genMethod(method) {
return (new Function(fnString))(); return (new Function(fnString))();
} }
function main(conf) { function main({ method, len, n, aligned = 'true' }) {
const method = conf.method || 'swap16';
const len = conf.len | 0;
const n = conf.n | 0;
const aligned = conf.aligned || 'true';
const buf = createBuffer(len, aligned === 'true'); const buf = createBuffer(len, aligned === 'true');
const bufferSwap = genMethod(method); const bufferSwap = genMethod(method || 'swap16');
bufferSwap(n, buf); bufferSwap(n, buf);
bench.start(); bench.start();

View File

@ -7,9 +7,8 @@ const bench = common.createBenchmark(main, {
len: [0, 10, 256, 4 * 1024] len: [0, 10, 256, 4 * 1024]
}); });
function main(conf) { function main({ n, len }) {
const n = +conf.n; const buf = Buffer.allocUnsafe(len);
const buf = Buffer.allocUnsafe(+conf.len);
bench.start(); bench.start();
for (var i = 0; i < n; ++i) for (var i = 0; i < n; ++i)

View File

@ -9,11 +9,7 @@ const bench = common.createBenchmark(main, {
n: [1e7] n: [1e7]
}); });
function main(conf) { function main({ encoding, args, len, n }) {
var encoding = conf.encoding;
const args = conf.args | 0;
const len = conf.len | 0;
const n = conf.n | 0;
const buf = Buffer.alloc(len, 42); const buf = Buffer.alloc(len, 42);
if (encoding.length === 0) if (encoding.length === 0)

View File

@ -10,12 +10,7 @@ const bench = common.createBenchmark(main, {
n: [1e7] n: [1e7]
}); });
function main(conf) { function main({ len, n, encoding, args }) {
const len = +conf.len;
const n = +conf.n;
const encoding = conf.encoding;
const args = conf.args;
const string = 'a'.repeat(len); const string = 'a'.repeat(len);
const buf = Buffer.allocUnsafe(len); const buf = Buffer.allocUnsafe(len);

View File

@ -45,13 +45,11 @@ const mod = {
writeUInt32LE: UINT32 writeUInt32LE: UINT32
}; };
function main(conf) { function main({ noAssert, millions, buf, type }) {
const noAssert = conf.noAssert === 'true'; const len = millions * 1e6;
const len = +conf.millions * 1e6; const clazz = buf === 'fast' ? Buffer : require('buffer').SlowBuffer;
const clazz = conf.buf === 'fast' ? Buffer : require('buffer').SlowBuffer;
const buff = new clazz(8); const buff = new clazz(8);
const type = conf.type || 'UInt8'; const fn = `write${type || 'UInt8'}`;
const fn = `write${type}`;
if (/Int/.test(fn)) if (/Int/.test(fn))
benchInt(buff, fn, len, noAssert); benchInt(buff, fn, len, noAssert);
@ -63,7 +61,7 @@ function benchInt(buff, fn, len, noAssert) {
const m = mod[fn]; const m = mod[fn];
const testFunction = new Function('buff', ` const testFunction = new Function('buff', `
for (var i = 0; i !== ${len}; i++) { for (var i = 0; i !== ${len}; i++) {
buff.${fn}(i & ${m}, 0, ${JSON.stringify(noAssert)}); buff.${fn}(i & ${m}, 0, ${noAssert});
} }
`); `);
bench.start(); bench.start();
@ -74,7 +72,7 @@ function benchInt(buff, fn, len, noAssert) {
function benchFloat(buff, fn, len, noAssert) { function benchFloat(buff, fn, len, noAssert) {
const testFunction = new Function('buff', ` const testFunction = new Function('buff', `
for (var i = 0; i !== ${len}; i++) { for (var i = 0; i !== ${len}; i++) {
buff.${fn}(i, 0, ${JSON.stringify(noAssert)}); buff.${fn}(i, 0, ${noAssert});
} }
`); `);
bench.start(); bench.start();

View File

@ -10,13 +10,12 @@ const bench = common.createBenchmark(main, {
const zeroBuffer = Buffer.alloc(0); const zeroBuffer = Buffer.alloc(0);
const zeroString = ''; const zeroString = '';
function main(conf) { function main({ n, type }) {
const n = +conf.n;
bench.start(); bench.start();
if (conf.type === 'buffer') if (type === 'buffer')
for (let i = 0; i < n * 1024; i++) Buffer.from(zeroBuffer); for (let i = 0; i < n * 1024; i++) Buffer.from(zeroBuffer);
else if (conf.type === 'string') else if (type === 'string')
for (let i = 0; i < n * 1024; i++) Buffer.from(zeroString); for (let i = 0; i < n * 1024; i++) Buffer.from(zeroString);
bench.end(n); bench.end(n);

View File

@ -39,11 +39,11 @@ const mod = {
setUint32: UINT32 setUint32: UINT32
}; };
function main(conf) { function main({ millions, type }) {
const len = +conf.millions * 1e6; type = type || 'Uint8';
const len = millions * 1e6;
const ab = new ArrayBuffer(8); const ab = new ArrayBuffer(8);
const dv = new DataView(ab, 0, 8); const dv = new DataView(ab, 0, 8);
const type = conf.type || 'Uint8';
const le = /LE$/.test(type); const le = /LE$/.test(type);
const fn = `set${type.replace(/[LB]E$/, '')}`; const fn = `set${type.replace(/[LB]E$/, '')}`;