Skip to content

Commit 08cd81b

Browse files
evanlucasMyles Borins
authored and
Myles Borins
committed
benchmark: add util.format benchmark
PR-URL: #5360 Reviewed-By: James M Snell <[email protected]>
1 parent 7d6acef commit 08cd81b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

benchmark/util/format.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
const util = require('util');
4+
const common = require('../common');
5+
const v8 = require('v8');
6+
const bench = common.createBenchmark(main, {
7+
n: [1e6]
8+
, type: ['string',
9+
'number',
10+
'object',
11+
'unknown',
12+
'no-replace']
13+
});
14+
15+
const inputs = {
16+
'string': ['Hello, my name is %s', 'fred'],
17+
'number': ['Hi, I was born in %d', 1942],
18+
'object': ['An error occurred %j', {msg: 'This is an error', code: 'ERR'}],
19+
'unknown': ['hello %a', 'test'],
20+
'no-replace': [1, 2]
21+
};
22+
23+
function main(conf) {
24+
const n = conf.n | 0;
25+
const type = conf.type;
26+
27+
const input = inputs[type];
28+
29+
v8.setFlagsFromString('--allow_natives_syntax');
30+
31+
util.format(input[0], input[1]);
32+
eval('%OptimizeFunctionOnNextCall(util.format)');
33+
util.format(input[0], input[1]);
34+
35+
bench.start();
36+
for (var i = 0; i < n; i++) {
37+
util.format(input[0], input[1]);
38+
}
39+
bench.end(n);
40+
}

0 commit comments

Comments
 (0)