Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cb13c7c

Browse filesBrowse files
BridgeARMylesBorins
authored andcommitted
benchmark: (buffer) refactor
PR-URL: #18320 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 9acf754 commit cb13c7c
Copy full SHA for cb13c7c

File tree

Expand file treeCollapse file tree

9 files changed

+56
-100
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

9 files changed

+56
-100
lines changed
Open diff view settings
Collapse file

‎benchmark/buffers/buffer-bytelength.js‎

Copy file name to clipboardExpand all lines: benchmark/buffers/buffer-bytelength.js
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ const chars = [
1717

1818
function main({ n, len, encoding }) {
1919
var strings = [];
20-
var results;
20+
var results = [ len * 16 ];
2121
if (encoding === 'buffer') {
2222
strings = [ Buffer.alloc(len * 16, 'a') ];
23-
results = [ len * 16 ];
2423
} else {
2524
for (const string of chars) {
2625
// Strings must be built differently, depending on encoding
Collapse file

‎benchmark/buffers/buffer-compare-offset.js‎

Copy file name to clipboardExpand all lines: benchmark/buffers/buffer-compare-offset.js
+4-8Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,22 @@ const bench = common.createBenchmark(main, {
88
});
99

1010
function compareUsingSlice(b0, b1, len, iter) {
11-
var i;
12-
bench.start();
13-
for (i = 0; i < iter; i++)
11+
for (var i = 0; i < iter; i++)
1412
Buffer.compare(b0.slice(1, len), b1.slice(1, len));
15-
bench.end(iter / 1e6);
1613
}
1714

1815
function compareUsingOffset(b0, b1, len, iter) {
19-
var i;
20-
bench.start();
21-
for (i = 0; i < iter; i++)
16+
for (var i = 0; i < iter; i++)
2217
b0.compare(b1, 1, len, 1, len);
23-
bench.end(iter / 1e6);
2418
}
2519

2620
function main({ millions, size, method }) {
2721
const iter = millions * 1e6;
2822
const fn = method === 'slice' ? compareUsingSlice : compareUsingOffset;
23+
bench.start();
2924
fn(Buffer.alloc(size, 'a'),
3025
Buffer.alloc(size, 'b'),
3126
size >> 1,
3227
iter);
28+
bench.end(millions);
3329
}
Collapse file

‎benchmark/buffers/buffer-creation.js‎

Copy file name to clipboardExpand all lines: benchmark/buffers/buffer-creation.js
+15-28Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,51 +16,38 @@ const bench = common.createBenchmark(main, {
1616
});
1717

1818
function main({ len, n, type }) {
19+
let fn, i;
1920
switch (type) {
2021
case '':
2122
case 'fast-alloc':
22-
bench.start();
23-
for (let i = 0; i < n * 1024; i++) {
24-
Buffer.alloc(len);
25-
}
26-
bench.end(n);
23+
fn = Buffer.alloc;
2724
break;
2825
case 'fast-alloc-fill':
2926
bench.start();
30-
for (let i = 0; i < n * 1024; i++) {
27+
for (i = 0; i < n * 1024; i++) {
3128
Buffer.alloc(len, 0);
3229
}
3330
bench.end(n);
34-
break;
31+
return;
3532
case 'fast-allocUnsafe':
36-
bench.start();
37-
for (let i = 0; i < n * 1024; i++) {
38-
Buffer.allocUnsafe(len);
39-
}
40-
bench.end(n);
33+
fn = Buffer.allocUnsafe;
4134
break;
4235
case 'slow-allocUnsafe':
43-
bench.start();
44-
for (let i = 0; i < n * 1024; i++) {
45-
Buffer.allocUnsafeSlow(len);
46-
}
47-
bench.end(n);
36+
fn = Buffer.allocUnsafeSlow;
4837
break;
4938
case 'slow':
50-
bench.start();
51-
for (let i = 0; i < n * 1024; i++) {
52-
SlowBuffer(len);
53-
}
54-
bench.end(n);
39+
fn = SlowBuffer;
5540
break;
5641
case 'buffer()':
57-
bench.start();
58-
for (let i = 0; i < n * 1024; i++) {
59-
Buffer(len);
60-
}
61-
bench.end(n);
42+
fn = Buffer;
6243
break;
6344
default:
64-
assert.fail(null, null, 'Should not get here');
45+
assert.fail('Should not get here');
46+
}
47+
48+
bench.start();
49+
for (i = 0; i < n * 1024; i++) {
50+
fn(len);
6551
}
52+
bench.end(n);
6653
}
Collapse file

‎benchmark/buffers/buffer-hex.js‎

Copy file name to clipboardExpand all lines: benchmark/buffers/buffer-hex.js
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ const bench = common.createBenchmark(main, {
99

1010
function main({ len, n }) {
1111
const buf = Buffer.alloc(len);
12+
var i;
1213

13-
for (let i = 0; i < buf.length; i++)
14+
for (i = 0; i < buf.length; i++)
1415
buf[i] = i & 0xff;
1516

1617
const hex = buf.toString('hex');
1718

1819
bench.start();
1920

20-
for (let i = 0; i < n; i += 1)
21+
for (i = 0; i < n; i += 1)
2122
Buffer.from(hex, 'hex');
2223

2324
bench.end(n);
Collapse file

‎benchmark/buffers/buffer-iterate.js‎

Copy file name to clipboardExpand all lines: benchmark/buffers/buffer-iterate.js
+5-13Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,30 @@ function main({ size, type, method, n }) {
2020
const clazz = type === 'fast' ? Buffer : SlowBuffer;
2121
const buffer = new clazz(size);
2222
buffer.fill(0);
23-
methods[method || 'for'](buffer, n);
24-
}
25-
23+
const fn = methods[method || 'for'];
2624

27-
function benchFor(buffer, n) {
2825
bench.start();
26+
fn(buffer, n);
27+
bench.end(n);
28+
}
2929

30+
function benchFor(buffer, n) {
3031
for (var k = 0; k < n; k++) {
3132
for (var i = 0; i < buffer.length; i++) {
3233
assert(buffer[i] === 0);
3334
}
3435
}
35-
36-
bench.end(n);
3736
}
3837

3938
function benchForOf(buffer, n) {
40-
bench.start();
41-
4239
for (var k = 0; k < n; k++) {
4340
for (const b of buffer) {
4441
assert(b === 0);
4542
}
4643
}
47-
bench.end(n);
4844
}
4945

5046
function benchIterator(buffer, n) {
51-
bench.start();
52-
5347
for (var k = 0; k < n; k++) {
5448
const iter = buffer[Symbol.iterator]();
5549
var cur = iter.next();
@@ -60,6 +54,4 @@ function benchIterator(buffer, n) {
6054
}
6155

6256
}
63-
64-
bench.end(n);
6557
}
Collapse file

‎benchmark/buffers/buffer-read-float.js‎

Copy file name to clipboardExpand all lines: benchmark/buffers/buffer-read-float.js
+9-14Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ const bench = common.createBenchmark(main, {
99
millions: [1]
1010
});
1111

12-
function main(conf) {
13-
const noAssert = conf.noAssert === 'true';
14-
const len = +conf.millions * 1e6;
12+
function main({ noAssert, millions, type, endian, value }) {
13+
noAssert = noAssert === 'true';
14+
type = type || 'Double';
1515
const buff = Buffer.alloc(8);
16-
const type = conf.type || 'Double';
17-
const endian = conf.endian;
1816
const fn = `read${type}${endian}`;
1917
const values = {
2018
Double: {
@@ -32,15 +30,12 @@ function main(conf) {
3230
nan: NaN,
3331
},
3432
};
35-
const value = values[type][conf.value];
3633

37-
buff[`write${type}${endian}`](value, 0, noAssert);
38-
const testFunction = new Function('buff', `
39-
for (var i = 0; i !== ${len}; i++) {
40-
buff.${fn}(0, ${JSON.stringify(noAssert)});
41-
}
42-
`);
34+
buff[`write${type}${endian}`](values[type][value], 0, noAssert);
35+
4336
bench.start();
44-
testFunction(buff);
45-
bench.end(len / 1e6);
37+
for (var i = 0; i !== millions * 1e6; i++) {
38+
buff[fn](0, noAssert);
39+
}
40+
bench.end(millions);
4641
}
Collapse file

‎benchmark/buffers/buffer-read.js‎

Copy file name to clipboardExpand all lines: benchmark/buffers/buffer-read.js
+4-8Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,14 @@ const bench = common.createBenchmark(main, {
2727

2828
function main({ noAssert, millions, buf, type }) {
2929
noAssert = noAssert === 'true';
30-
const len = millions * 1e6;
3130
const clazz = buf === 'fast' ? Buffer : require('buffer').SlowBuffer;
3231
const buff = new clazz(8);
3332
const fn = `read${type || 'UInt8'}`;
3433

3534
buff.writeDoubleLE(0, 0, noAssert);
36-
const testFunction = new Function('buff', `
37-
for (var i = 0; i !== ${len}; i++) {
38-
buff.${fn}(0, ${JSON.stringify(noAssert)});
39-
}
40-
`);
4135
bench.start();
42-
testFunction(buff);
43-
bench.end(len / 1e6);
36+
for (var i = 0; i !== millions * 1e6; i++) {
37+
buff[fn](0, noAssert);
38+
}
39+
bench.end(millions);
4440
}
Collapse file

‎benchmark/buffers/buffer-write.js‎

Copy file name to clipboardExpand all lines: benchmark/buffers/buffer-write.js
+12-19Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,36 +46,29 @@ const mod = {
4646
};
4747

4848
function main({ noAssert, millions, buf, type }) {
49-
const len = millions * 1e6;
5049
const clazz = buf === 'fast' ? Buffer : require('buffer').SlowBuffer;
5150
const buff = new clazz(8);
5251
const fn = `write${type || 'UInt8'}`;
5352

5453
if (/Int/.test(fn))
55-
benchInt(buff, fn, len, noAssert);
54+
benchInt(buff, fn, millions, noAssert);
5655
else
57-
benchFloat(buff, fn, len, noAssert);
56+
benchFloat(buff, fn, millions, noAssert);
5857
}
5958

60-
function benchInt(buff, fn, len, noAssert) {
59+
function benchInt(buff, fn, millions, noAssert) {
6160
const m = mod[fn];
62-
const testFunction = new Function('buff', `
63-
for (var i = 0; i !== ${len}; i++) {
64-
buff.${fn}(i & ${m}, 0, ${noAssert});
65-
}
66-
`);
6761
bench.start();
68-
testFunction(buff);
69-
bench.end(len / 1e6);
62+
for (var i = 0; i !== millions * 1e6; i++) {
63+
buff[fn](i & m, 0, noAssert);
64+
}
65+
bench.end(millions);
7066
}
7167

72-
function benchFloat(buff, fn, len, noAssert) {
73-
const testFunction = new Function('buff', `
74-
for (var i = 0; i !== ${len}; i++) {
75-
buff.${fn}(i, 0, ${noAssert});
76-
}
77-
`);
68+
function benchFloat(buff, fn, millions, noAssert) {
7869
bench.start();
79-
testFunction(buff);
80-
bench.end(len / 1e6);
70+
for (var i = 0; i !== millions * 1e6; i++) {
71+
buff[fn](i, 0, noAssert);
72+
}
73+
bench.end(millions);
8174
}
Collapse file

‎benchmark/buffers/buffer_zero.js‎

Copy file name to clipboardExpand all lines: benchmark/buffers/buffer_zero.js
+3-6Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@ const zeroBuffer = Buffer.alloc(0);
1111
const zeroString = '';
1212

1313
function main({ n, type }) {
14-
bench.start();
15-
16-
if (type === 'buffer')
17-
for (let i = 0; i < n * 1024; i++) Buffer.from(zeroBuffer);
18-
else if (type === 'string')
19-
for (let i = 0; i < n * 1024; i++) Buffer.from(zeroString);
14+
const data = type === 'buffer' ? zeroBuffer : zeroString;
2015

16+
bench.start();
17+
for (var i = 0; i < n * 1024; i++) Buffer.from(data);
2118
bench.end(n);
2219
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.