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],
});
function main(conf) {
const n = +conf.n;
function main({ n }) {
const charsPerLine = 76;
const linesCount = 8 << 16;
const bytesCount = charsPerLine * linesCount / 4 * 3;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -8,15 +8,11 @@ const bench = common.createBenchmark(main, {
n: [1024]
});
function main(conf) {
const n = +conf.n;
const size = +conf.pieceSize;
const pieces = +conf.pieces;
function main({ n, pieces, pieceSize, withTotalLength }) {
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();
for (var i = 0; i < n * 1024; i++) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -10,13 +10,12 @@ const bench = common.createBenchmark(main, {
const zeroBuffer = Buffer.alloc(0);
const zeroString = '';
function main(conf) {
const n = +conf.n;
function main({ n, type }) {
bench.start();
if (conf.type === 'buffer')
if (type === 'buffer')
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);
bench.end(n);

View File

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