-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmark: add util.format benchmark
PR-URL: #5360 Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
1 parent
86900f8
commit 12ca84f
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
const util = require('util'); | ||
const common = require('../common'); | ||
const v8 = require('v8'); | ||
const bench = common.createBenchmark(main, { | ||
n: [1e6] | ||
, type: ['string', | ||
'number', | ||
'object', | ||
'unknown', | ||
'no-replace'] | ||
}); | ||
|
||
const inputs = { | ||
'string': ['Hello, my name is %s', 'fred'], | ||
'number': ['Hi, I was born in %d', 1942], | ||
'object': ['An error occurred %j', {msg: 'This is an error', code: 'ERR'}], | ||
'unknown': ['hello %a', 'test'], | ||
'no-replace': [1, 2] | ||
}; | ||
|
||
function main(conf) { | ||
const n = conf.n | 0; | ||
const type = conf.type; | ||
|
||
const input = inputs[type]; | ||
|
||
v8.setFlagsFromString('--allow_natives_syntax'); | ||
|
||
util.format(input[0], input[1]); | ||
eval('%OptimizeFunctionOnNextCall(util.format)'); | ||
util.format(input[0], input[1]); | ||
|
||
bench.start(); | ||
for (var i = 0; i < n; i++) { | ||
util.format(input[0], input[1]); | ||
} | ||
bench.end(n); | ||
} |