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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 18 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 5 additions & 13 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 13 additions & 12 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 2 deletions
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
}

0 commit comments

Comments
 (0)