0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/benchmark/util/format.js
Sarah Meyer 50fe1a8409 tools, benchmark: test util benchmark
Create a minimal test for the util benchmark files.

PR-URL: https://github.com/nodejs/node/pull/16050
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
2017-10-28 13:56:44 -07:00

35 lines
920 B
JavaScript

'use strict';
const util = require('util');
const common = require('../common');
const inputs = {
'string': ['Hello, my name is %s', 'Fred'],
'string-2': ['Hello, %s is my name', 'Fred'],
'number': ['Hi, I was born in %d', 1989],
'replace-object': ['An error occurred %j', { msg: 'This is an error' }],
'unknown': ['hello %a', 'test'],
'no-replace': [1, 2],
'no-replace-2': ['foobar', 'yeah', 'mensch', 5],
'only-objects': [{ msg: 'This is an error' }, { msg: 'This is an error' }],
'many-%': ['replace%%%%s%%%%many%s%s%s', 'percent'],
};
const bench = common.createBenchmark(main, {
n: [4e6],
type: Object.keys(inputs)
});
function main({ n, type }) {
// For testing, if supplied with an empty type, default to string.
type = type || 'string';
const [first, second] = inputs[type];
bench.start();
for (var i = 0; i < n; i++) {
util.format(first, second);
}
bench.end(n);
}