Skip to content

Commit db65594

Browse files
BridgeARtargos
authored andcommitted
benchmark: refactor buffer benchmarks
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: #26418 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Peter Marshall <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 337aef0 commit db65594

23 files changed

+80
-105
lines changed

benchmark/buffers/buffer-bytelength.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ const common = require('../common');
33

44
const bench = common.createBenchmark(main, {
55
encoding: ['utf8', 'base64', 'buffer'],
6-
len: [1, 2, 4, 16, 64, 256], // x16
7-
n: [5e6]
6+
len: [2, 16, 256], // x16
7+
n: [4e6]
88
});
99

1010
// 16 chars each

benchmark/buffers/buffer-compare-instance-method.js

+2-18
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
const common = require('../common.js');
33

44
const bench = common.createBenchmark(main, {
5-
size: [16, 512, 1024, 4096, 16386],
6-
args: [1, 2, 3, 4, 5],
5+
size: [16, 512, 4096, 16386],
6+
args: [1, 2, 5],
77
n: [1e6]
88
});
99

@@ -16,22 +16,6 @@ function main({ n, size, args }) {
1616

1717
b1[size - 1] = 'b'.charCodeAt(0);
1818

19-
switch (args) {
20-
case 2:
21-
b0.compare(b1, 0);
22-
break;
23-
case 3:
24-
b0.compare(b1, 0, b1Len);
25-
break;
26-
case 4:
27-
b0.compare(b1, 0, b1Len, 0);
28-
break;
29-
case 5:
30-
b0.compare(b1, 0, b1Len, 0, b0Len);
31-
break;
32-
default:
33-
b0.compare(b1);
34-
}
3519
switch (args) {
3620
case 2:
3721
b0.compare(b1, 0);

benchmark/buffers/buffer-compare-offset.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const common = require('../common.js');
33

44
const bench = common.createBenchmark(main, {
55
method: ['offset', 'slice'],
6-
size: [16, 512, 1024, 4096, 16386],
6+
size: [16, 512, 4096, 16386],
77
n: [1e6]
88
});
99

benchmark/buffers/buffer-compare.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
const common = require('../common.js');
2424

2525
const bench = common.createBenchmark(main, {
26-
size: [16, 512, 1024, 4096, 16386],
26+
size: [16, 512, 4096, 16386],
2727
n: [1e6]
2828
});
2929

benchmark/buffers/buffer-concat.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
const common = require('../common.js');
33

44
const bench = common.createBenchmark(main, {
5-
pieces: [1, 4, 16],
5+
pieces: [4, 16],
66
pieceSize: [1, 16, 256],
77
withTotalLength: [0, 1],
8-
n: [1024]
8+
n: [8e5]
99
});
1010

1111
function main({ n, pieces, pieceSize, withTotalLength }) {
12-
const list = new Array(pieces);
13-
list.fill(Buffer.allocUnsafe(pieceSize));
12+
const list = Array.from({ length: pieces })
13+
.fill(Buffer.allocUnsafe(pieceSize));
1414

1515
const totalLength = withTotalLength ? pieces * pieceSize : undefined;
1616

1717
bench.start();
18-
for (var i = 0; i < n * 1024; i++) {
18+
for (var i = 0; i < n; i++) {
1919
Buffer.concat(list, totalLength);
2020
}
2121
bench.end(n);

benchmark/buffers/buffer-creation.js

+5-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
const SlowBuffer = require('buffer').SlowBuffer;
32

43
const common = require('../common.js');
54
const assert = require('assert');
@@ -9,10 +8,9 @@ const bench = common.createBenchmark(main, {
98
'fast-alloc-fill',
109
'fast-allocUnsafe',
1110
'slow-allocUnsafe',
12-
'slow',
13-
'buffer()'],
14-
len: [10, 1024, 2048, 4096, 8192],
15-
n: [1024]
11+
],
12+
len: [10, 1024, 4096, 8192],
13+
n: [6e5]
1614
});
1715

1816
function main({ len, n, type }) {
@@ -24,7 +22,7 @@ function main({ len, n, type }) {
2422
break;
2523
case 'fast-alloc-fill':
2624
bench.start();
27-
for (i = 0; i < n * 1024; i++) {
25+
for (i = 0; i < n; i++) {
2826
Buffer.alloc(len, 0);
2927
}
3028
bench.end(n);
@@ -35,18 +33,12 @@ function main({ len, n, type }) {
3533
case 'slow-allocUnsafe':
3634
fn = Buffer.allocUnsafeSlow;
3735
break;
38-
case 'slow':
39-
fn = SlowBuffer;
40-
break;
41-
case 'buffer()':
42-
fn = Buffer;
43-
break;
4436
default:
4537
assert.fail('Should not get here');
4638
}
4739

4840
bench.start();
49-
for (i = 0; i < n * 1024; i++) {
41+
for (i = 0; i < n; i++) {
5042
fn(len);
5143
}
5244
bench.end(n);

benchmark/buffers/buffer-fill.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const bench = common.createBenchmark(main, {
1414
'fill("t", 0)',
1515
'fill(Buffer.alloc(1), 0)',
1616
],
17-
size: [2 ** 8, 2 ** 13, 2 ** 16],
17+
size: [2 ** 13, 2 ** 16],
1818
n: [2e4]
1919
});
2020

benchmark/buffers/buffer-from.js

+13-12
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ const bench = common.createBenchmark(main, {
88
'arraybuffer',
99
'arraybuffer-middle',
1010
'buffer',
11-
'uint8array',
1211
'string',
1312
'string-utf8',
1413
'string-base64',
1514
'object',
1615
],
17-
len: [10, 2048],
18-
n: [2048]
16+
len: [100, 2048],
17+
n: [8e5]
1918
});
2019

2120
function main({ len, n, source }) {
@@ -26,17 +25,19 @@ function main({ len, n, source }) {
2625
const uint8array = new Uint8Array(len);
2726
const obj = { length: null }; // Results in a new, empty Buffer
2827

28+
let i = 0;
29+
2930
switch (source) {
3031
case 'array':
3132
bench.start();
32-
for (let i = 0; i < n * 1024; i++) {
33+
for (i = 0; i < n; i++) {
3334
Buffer.from(array);
3435
}
3536
bench.end(n);
3637
break;
3738
case 'arraybuffer':
3839
bench.start();
39-
for (let i = 0; i < n * 1024; i++) {
40+
for (i = 0; i < n; i++) {
4041
Buffer.from(arrayBuf);
4142
}
4243
bench.end(n);
@@ -45,49 +46,49 @@ function main({ len, n, source }) {
4546
const offset = ~~(len / 4);
4647
const length = ~~(len / 2);
4748
bench.start();
48-
for (let i = 0; i < n * 1024; i++) {
49+
for (i = 0; i < n; i++) {
4950
Buffer.from(arrayBuf, offset, length);
5051
}
5152
bench.end(n);
5253
break;
5354
case 'buffer':
5455
bench.start();
55-
for (let i = 0; i < n * 1024; i++) {
56+
for (i = 0; i < n; i++) {
5657
Buffer.from(buffer);
5758
}
5859
bench.end(n);
5960
break;
6061
case 'uint8array':
6162
bench.start();
62-
for (let i = 0; i < n * 1024; i++) {
63+
for (i = 0; i < n; i++) {
6364
Buffer.from(uint8array);
6465
}
6566
bench.end(n);
6667
break;
6768
case 'string':
6869
bench.start();
69-
for (let i = 0; i < n * 1024; i++) {
70+
for (i = 0; i < n; i++) {
7071
Buffer.from(str);
7172
}
7273
bench.end(n);
7374
break;
7475
case 'string-utf8':
7576
bench.start();
76-
for (let i = 0; i < n * 1024; i++) {
77+
for (i = 0; i < n; i++) {
7778
Buffer.from(str, 'utf8');
7879
}
7980
bench.end(n);
8081
break;
8182
case 'string-base64':
8283
bench.start();
83-
for (let i = 0; i < n * 1024; i++) {
84+
for (i = 0; i < n; i++) {
8485
Buffer.from(str, 'base64');
8586
}
8687
bench.end(n);
8788
break;
8889
case 'object':
8990
bench.start();
90-
for (let i = 0; i < n * 1024; i++) {
91+
for (i = 0; i < n; i++) {
9192
Buffer.from(obj);
9293
}
9394
bench.end(n);

benchmark/buffers/buffer-hex.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
const common = require('../common.js');
44

55
const bench = common.createBenchmark(main, {
6-
len: [0, 1, 64, 1024],
7-
n: [1e7]
6+
len: [64, 1024],
7+
n: [1e6]
88
});
99

1010
function main({ len, n }) {

benchmark/buffers/buffer-indexof-number.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ const path = require('path');
55

66
const bench = common.createBenchmark(main, {
77
value: ['@'.charCodeAt(0)],
8-
n: [1e7]
8+
n: [1e6]
99
});
1010

1111
function main({ n, value }) {
1212
const aliceBuffer = fs.readFileSync(
1313
path.resolve(__dirname, '../fixtures/alice.html')
1414
);
1515

16+
let count = 0;
1617
bench.start();
1718
for (var i = 0; i < n; i++) {
18-
aliceBuffer.indexOf(value, 0, undefined);
19+
count += aliceBuffer.indexOf(value, 0, undefined);
1920
}
2021
bench.end(n);
22+
return count;
2123
}

benchmark/buffers/buffer-indexof.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,22 @@ const path = require('path');
66
const searchStrings = [
77
'@',
88
'SQ',
9-
'10x',
109
'--l',
1110
'Alice',
1211
'Gryphon',
13-
'Panther',
1412
'Ou est ma chatte?',
1513
'found it very',
16-
'among mad people',
1714
'neighbouring pool',
18-
'Soo--oop',
1915
'aaaaaaaaaaaaaaaaa',
2016
'venture to go near the house till she had brought herself down to',
2117
'</i> to the Caterpillar',
2218
];
2319

2420
const bench = common.createBenchmark(main, {
2521
search: searchStrings,
26-
encoding: ['undefined', 'utf8', 'ucs2', 'binary'],
22+
encoding: ['utf8', 'ucs2'],
2723
type: ['buffer', 'string'],
28-
n: [100000]
24+
n: [5e4]
2925
});
3026

3127
function main({ n, search, encoding, type }) {

benchmark/buffers/buffer-iterate.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ const common = require('../common.js');
44
const assert = require('assert');
55

66
const bench = common.createBenchmark(main, {
7-
size: [16, 512, 1024, 4096, 16386],
8-
type: ['fast', 'slow'],
7+
size: [512, 4096, 16386],
8+
type: ['fast'],
99
method: ['for', 'forOf', 'iterator'],
1010
n: [1e3]
1111
});
@@ -17,9 +17,10 @@ const methods = {
1717
};
1818

1919
function main({ size, type, method, n }) {
20-
const clazz = type === 'fast' ? Buffer : SlowBuffer;
21-
const buffer = new clazz(size);
22-
buffer.fill(0);
20+
const buffer = type === 'fast' ?
21+
Buffer.alloc(size) :
22+
SlowBuffer(size).fill(0);
23+
2324
const fn = methods[method || 'for'];
2425

2526
bench.start();
@@ -46,7 +47,7 @@ function benchForOf(buffer, n) {
4647
function benchIterator(buffer, n) {
4748
for (var k = 0; k < n; k++) {
4849
const iter = buffer[Symbol.iterator]();
49-
var cur = iter.next();
50+
let cur = iter.next();
5051

5152
while (!cur.done) {
5253
assert(cur.value === 0);

benchmark/buffers/buffer-normalize-encoding.js

-5
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,18 @@ const common = require('../common.js');
55
const bench = common.createBenchmark(main, {
66
encoding: [
77
'ascii',
8-
'ASCII',
98
'base64',
109
'BASE64',
1110
'binary',
12-
'BINARY',
1311
'hex',
1412
'HEX',
1513
'latin1',
1614
'LATIN1',
17-
'ucs-2',
1815
'UCS-2',
19-
'ucs2',
2016
'UCS2',
2117
'utf-16le',
2218
'UTF-16LE',
2319
'utf-8',
24-
'UTF-8',
2520
'utf16le',
2621
'UTF16LE',
2722
'utf8',

benchmark/buffers/buffer-read-float.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const common = require('../common.js');
33

44
const bench = common.createBenchmark(main, {
55
type: ['Double', 'Float'],
6-
endian: ['BE', 'LE'],
6+
endian: ['LE'],
77
value: ['zero', 'big', 'small', 'inf', 'nan'],
88
n: [1e6]
99
});

benchmark/buffers/buffer-read-with-byteLength.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ const types = [
99
];
1010

1111
const bench = common.createBenchmark(main, {
12-
buffer: ['fast', 'slow'],
12+
buffer: ['fast'],
1313
type: types,
1414
n: [1e6],
1515
byteLength: [1, 2, 3, 4, 5, 6]
1616
});
1717

1818
function main({ n, buf, type, byteLength }) {
19-
const clazz = buf === 'fast' ? Buffer : require('buffer').SlowBuffer;
20-
const buff = new clazz(8);
19+
const buff = buf === 'fast' ?
20+
Buffer.alloc(8) :
21+
require('buffer').SlowBuffer(8);
2122
const fn = `read${type || 'IntBE'}`;
2223

2324
buff.writeDoubleLE(0, 0);

0 commit comments

Comments
 (0)