• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common.js');
4
5const configs = {
6  n: [1e3],
7  mode: [
8    'multi-concat',
9    'multi-join',
10    'multi-template',
11    'to-string-string',
12    'to-string-concat',
13    'to-string-template',
14  ],
15};
16
17const bench = common.createBenchmark(main, configs);
18
19function main({ n, mode }) {
20  const str = 'abc';
21  const num = 123;
22
23  let string;
24
25  switch (mode) {
26    case 'multi-concat':
27      bench.start();
28      for (let i = 0; i < n; i++)
29        string = '...' + str + ', ' + num + ', ' + str + ', ' + num + '.';
30      bench.end(n);
31      break;
32    case 'multi-join':
33      bench.start();
34      for (let i = 0; i < n; i++)
35        string = ['...', str, ', ', num, ', ', str, ', ', num, '.'].join('');
36      bench.end(n);
37      break;
38    case 'multi-template':
39      bench.start();
40      for (let i = 0; i < n; i++)
41        string = `...${str}, ${num}, ${str}, ${num}.`;
42      bench.end(n);
43      break;
44    case 'to-string-string':
45      bench.start();
46      for (let i = 0; i < n; i++)
47        string = String(num);
48      bench.end(n);
49      break;
50    case 'to-string-concat':
51      bench.start();
52      for (let i = 0; i < n; i++)
53        string = '' + num;
54      bench.end(n);
55      break;
56    case 'to-string-template':
57      bench.start();
58      for (let i = 0; i < n; i++)
59        string = `${num}`;
60      bench.end(n);
61      break;
62    default:
63      throw new Error(`Unexpected method "${mode}"`);
64  }
65
66  return string;
67}
68