• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const util = require('util');
4const common = require('../common');
5
6const inputs = {
7  'string': ['Hello, my name is %s', 'Fred'],
8  'string-2': ['Hello, %s is my name', 'Fred'],
9  'number': ['Hi, I was born in %d', 1989],
10  'replace-object': ['An error occurred %j', { msg: 'This is an error' }],
11  'unknown': ['hello %a', 'test'],
12  'no-replace': [1, 2],
13  'no-replace-2': ['foobar', 'yeah', 'mensch', 5],
14  'only-objects': [{ msg: 'This is an error' }, { msg: 'This is an error' }],
15  'many-%': ['replace%%%%s%%%%many%s%s%s', 'percent'],
16  'object-to-string': ['foo %s bar', { toString() { return 'bla'; } }],
17  'object-%s': ['foo %s bar', { a: true, b: false }],
18};
19
20const bench = common.createBenchmark(main, {
21  n: [1e5],
22  type: Object.keys(inputs)
23});
24
25function main({ n, type }) {
26  const [first, second] = inputs[type];
27
28  bench.start();
29  for (let i = 0; i < n; i++) {
30    util.format(first, second);
31  }
32  bench.end(n);
33}
34