Skip to content

Commit

Permalink
benchmark: (buffers) use destructuring
Browse files Browse the repository at this point in the history
PR-URL: #18250
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
BridgeAR authored and evanlucas committed Jan 30, 2018
1 parent 71faa5c commit 5cf5ab1
Show file tree
Hide file tree
Showing 23 changed files with 57 additions and 108 deletions.
3 changes: 1 addition & 2 deletions benchmark/buffers/buffer-base64-decode-wrapped.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions benchmark/buffers/buffer-base64-decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions benchmark/buffers/buffer-base64-encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 1 addition & 5 deletions benchmark/buffers/buffer-bytelength.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
6 changes: 2 additions & 4 deletions benchmark/buffers/buffer-compare-instance-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 7 additions & 9 deletions benchmark/buffers/buffer-compare-offset.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
5 changes: 2 additions & 3 deletions benchmark/buffers/buffer-compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
10 changes: 3 additions & 7 deletions benchmark/buffers/buffer-concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
6 changes: 2 additions & 4 deletions benchmark/buffers/buffer-creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 2 additions & 5 deletions benchmark/buffers/buffer-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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++) {
Expand Down
4 changes: 1 addition & 3 deletions benchmark/buffers/buffer-hex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down
6 changes: 2 additions & 4 deletions benchmark/buffers/buffer-indexof-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
9 changes: 3 additions & 6 deletions benchmark/buffers/buffer-indexof.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down
11 changes: 4 additions & 7 deletions benchmark/buffers/buffer-iterate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
11 changes: 5 additions & 6 deletions benchmark/buffers/buffer-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', `
Expand Down
5 changes: 2 additions & 3 deletions benchmark/buffers/buffer-slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 2 additions & 6 deletions benchmark/buffers/buffer-swap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 2 additions & 3 deletions benchmark/buffers/buffer-tojson.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions benchmark/buffers/buffer-tostring.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 1 addition & 6 deletions benchmark/buffers/buffer-write-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
14 changes: 6 additions & 8 deletions benchmark/buffers/buffer-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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();
Expand Down
7 changes: 3 additions & 4 deletions benchmark/buffers/buffer_zero.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions benchmark/buffers/dataview-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -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$/, '')}`;

Expand Down

0 comments on commit 5cf5ab1

Please sign in to comment.