Skip to content

Commit 0f8f37e

Browse files
BridgeARjasnell
authored andcommitted
benchmark: improve and add more inspect benchmarks
PR-URL: #14881 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent cf1fe76 commit 0f8f37e

File tree

5 files changed

+103
-46
lines changed

5 files changed

+103
-46
lines changed

benchmark/util/format.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const types = [
1010
'no-replace'
1111
];
1212
const bench = common.createBenchmark(main, {
13-
n: [1e6],
13+
n: [2e6],
1414
type: types
1515
});
1616

benchmark/util/inspect-array.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@ const common = require('../common');
44
const util = require('util');
55

66
const bench = common.createBenchmark(main, {
7-
n: [1e2],
7+
n: [1e3],
88
len: [1e5],
99
type: [
1010
'denseArray',
1111
'sparseArray',
12-
'mixedArray'
12+
'mixedArray',
13+
'denseArray_showHidden',
1314
]
1415
});
1516

16-
function main(conf) {
17-
const { n, len, type } = conf;
17+
function main({ n, len, type }) {
1818
var arr = Array(len);
19-
var i;
19+
var i, opts;
2020

2121
switch (type) {
22+
case 'denseArray_showHidden':
23+
opts = { showHidden: true };
24+
arr = arr.fill('denseArray');
25+
break;
2226
case 'denseArray':
23-
arr = arr.fill(0);
27+
arr = arr.fill('denseArray');
2428
break;
2529
case 'sparseArray':
2630
break;
@@ -33,7 +37,7 @@ function main(conf) {
3337
}
3438
bench.start();
3539
for (i = 0; i < n; i++) {
36-
util.inspect(arr);
40+
util.inspect(arr, opts);
3741
}
3842
bench.end(n);
3943
}

benchmark/util/inspect-proxy.js

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,13 @@
33
const util = require('util');
44
const common = require('../common.js');
55

6-
const bench = common.createBenchmark(main, {
7-
v: [1, 2],
8-
n: [1e6]
9-
});
6+
const bench = common.createBenchmark(main, { n: [1e6] });
107

11-
function twoDifferentProxies(n) {
12-
// This one should be slower because we're looking up multiple proxies.
8+
function main({ n }) {
139
const proxyA = new Proxy({}, { get: () => {} });
14-
const proxyB = new Proxy({}, { get: () => {} });
10+
const proxyB = new Proxy(() => {}, {});
1511
bench.start();
1612
for (var i = 0; i < n; i += 1)
1713
util.inspect({ a: proxyA, b: proxyB }, { showProxy: true });
1814
bench.end(n);
1915
}
20-
21-
function oneProxy(n) {
22-
// This one should be a bit faster because of the internal caching.
23-
const proxy = new Proxy({}, { get: () => {} });
24-
bench.start();
25-
for (var i = 0; i < n; i += 1)
26-
util.inspect({ a: proxy, b: proxy }, { showProxy: true });
27-
bench.end(n);
28-
}
29-
30-
function main(conf) {
31-
const n = conf.n | 0;
32-
const v = conf.v | 0;
33-
34-
switch (v) {
35-
case 1:
36-
oneProxy(n);
37-
break;
38-
case 2:
39-
twoDifferentProxies(n);
40-
break;
41-
default:
42-
throw new Error('Should not get to here');
43-
}
44-
}

benchmark/util/inspect.js

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,96 @@ var util = require('util');
33

44
var common = require('../common.js');
55

6-
var bench = common.createBenchmark(main, { n: [5e6] });
7-
8-
function main(conf) {
9-
var n = conf.n | 0;
6+
const opts = {
7+
showHidden: { showHidden: true },
8+
colors: { colors: true },
9+
none: undefined
10+
};
11+
var bench = common.createBenchmark(main, {
12+
n: [2e6],
13+
method: [
14+
'Object',
15+
'Object_empty',
16+
'Object_deep_ln',
17+
'String',
18+
'String_complex',
19+
'String_boxed',
20+
'Date',
21+
'Set',
22+
'Error',
23+
'Array',
24+
'TypedArray',
25+
'TypedArray_extra'
26+
],
27+
option: Object.keys(opts)
28+
});
1029

30+
function benchmark(n, obj, options) {
1131
bench.start();
1232
for (var i = 0; i < n; i += 1) {
13-
util.inspect({ a: 'a', b: 'b', c: 'c', d: 'd' });
33+
util.inspect(obj, options);
1434
}
1535
bench.end(n);
1636
}
37+
38+
function main({ method, n, option }) {
39+
var obj;
40+
const options = opts[option];
41+
switch (method) {
42+
case 'Object':
43+
benchmark(n, { a: 'a', b: 'b', c: 'c', d: 'd' }, options);
44+
break;
45+
case 'Object_empty':
46+
benchmark(n, {}, options);
47+
break;
48+
case 'Object_deep_ln':
49+
if (options)
50+
options.depth = Infinity;
51+
obj = { first:
52+
{ second:
53+
{ third:
54+
{ a: 'first',
55+
b: 'second',
56+
c: 'third',
57+
d: 'fourth',
58+
e: 'fifth',
59+
f: 'sixth',
60+
g: 'seventh' } } } };
61+
benchmark(n, obj, options || { depth: Infinity });
62+
break;
63+
case 'String':
64+
benchmark(n, 'Simple string', options);
65+
break;
66+
case 'String_complex':
67+
benchmark(n, 'This string\nhas to be\tescaped!', options);
68+
break;
69+
case 'String_boxed':
70+
benchmark(n, new String('string'), options);
71+
break;
72+
case 'Date':
73+
benchmark(n, new Date(), options);
74+
break;
75+
case 'Set':
76+
obj = new Set([5, 3]);
77+
benchmark(n, obj, options);
78+
break;
79+
case 'Error':
80+
benchmark(n, new Error('error'), options);
81+
break;
82+
case 'Array':
83+
benchmark(n, Array(20).fill().map((_, i) => i), options);
84+
break;
85+
case 'TypedArray':
86+
obj = new Uint8Array(Array(50).fill().map((_, i) => i));
87+
benchmark(n, obj, options);
88+
break;
89+
case 'TypedArray_extra':
90+
obj = new Uint8Array(Array(50).fill().map((_, i) => i));
91+
obj.foo = 'bar';
92+
obj[Symbol('baz')] = 5;
93+
benchmark(n, obj, options);
94+
break;
95+
default:
96+
throw new Error(`Unsupported method "${method}"`);
97+
}
98+
}

benchmark/util/normalize-encoding.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const inputs = [
1919

2020
const bench = common.createBenchmark(main, {
2121
input: inputs.concat(Object.keys(groupedInputs)),
22-
n: [1e5]
22+
n: [1e7]
2323
}, {
2424
flags: '--expose-internals'
2525
});

0 commit comments

Comments
 (0)