Skip to content

Commit 94d6487

Browse files
TrottBridgeAR
authored andcommitted
benchmark: improve buffer.readInt(B|L)E benchmarks
Split them into their own benhmark file and use different byteLength values. PR-URL: #11146 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent d964ffe commit 94d6487

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
4+
const types = [
5+
'IntLE',
6+
'IntBE',
7+
];
8+
9+
const bench = common.createBenchmark(main, {
10+
noAssert: ['false', 'true'],
11+
buffer: ['fast', 'slow'],
12+
type: types,
13+
millions: [1],
14+
byteLength: [1, 2, 4, 6]
15+
});
16+
17+
function main(conf) {
18+
const noAssert = conf.noAssert === 'true';
19+
const len = conf.millions * 1e6;
20+
const clazz = conf.buf === 'fast' ? Buffer : require('buffer').SlowBuffer;
21+
const buff = new clazz(8);
22+
const type = conf.type || 'UInt8';
23+
const fn = `read${type}`;
24+
25+
buff.writeDoubleLE(0, 0, noAssert);
26+
const testFunction = new Function('buff', `
27+
for (var i = 0; i !== ${len}; i++) {
28+
buff.${fn}(0, ${conf.byteLength}, ${JSON.stringify(noAssert)});
29+
}
30+
`);
31+
bench.start();
32+
testFunction(buff);
33+
bench.end(len / 1e6);
34+
}

benchmark/buffers/buffer-read.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ const types = [
1515
'FloatLE',
1616
'FloatBE',
1717
'DoubleLE',
18-
'DoubleBE',
19-
'IntLE',
20-
'IntBE',
18+
'DoubleBE'
2119
];
2220

2321
const bench = common.createBenchmark(main, {
@@ -36,19 +34,11 @@ function main(conf) {
3634
const fn = `read${type}`;
3735

3836
buff.writeDoubleLE(0, 0, noAssert);
39-
40-
var call;
41-
if (fn === 'readIntLE' || fn === 'readIntBE') {
42-
call = `buff.${fn}(0, 1, ${JSON.stringify(noAssert)})`;
43-
} else {
44-
call = `buff.${fn}(0, ${JSON.stringify(noAssert)})`;
45-
}
46-
47-
const testFunction = new Function(
48-
'buff',
49-
`for (var i = 0; i !== ${len}; ++i) { ${call}; }`
50-
);
51-
37+
const testFunction = new Function('buff', `
38+
for (var i = 0; i !== ${len}; i++) {
39+
buff.${fn}(0, ${JSON.stringify(noAssert)});
40+
}
41+
`);
5242
bench.start();
5343
testFunction(buff);
5444
bench.end(len / 1e6);

test/sequential/test-benchmark-buffer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ runBenchmark('buffers',
99
'aligned=true',
1010
'args=1',
1111
'buffer=fast',
12+
'byteLength=1',
1213
'encoding=utf8',
14+
'endian=BE',
1315
'len=2',
1416
'method=',
1517
'n=1',
@@ -20,6 +22,7 @@ runBenchmark('buffers',
2022
'size=1',
2123
'source=array',
2224
'type=',
25+
'value=0',
2326
'withTotalLength=0'
2427
],
2528
{ NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });

0 commit comments

Comments
 (0)