mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.4 KiB
69 lines
1.4 KiB
'use strict';
|
|
|
|
const common = require('../common.js');
|
|
|
|
const configs = {
|
|
n: [1e3],
|
|
mode: [
|
|
'multi-concat',
|
|
'multi-join',
|
|
'multi-template',
|
|
'to-string-string',
|
|
'to-string-concat',
|
|
'to-string-template',
|
|
],
|
|
};
|
|
|
|
const bench = common.createBenchmark(main, configs);
|
|
|
|
|
|
function main(conf) {
|
|
const n = +conf.n;
|
|
const mode = conf.mode;
|
|
|
|
const str = 'abc';
|
|
const num = 123;
|
|
|
|
let string;
|
|
|
|
switch (mode) {
|
|
case 'multi-concat':
|
|
bench.start();
|
|
for (let i = 0; i < n; i++)
|
|
string = '...' + str + ', ' + num + ', ' + str + ', ' + num + '.';
|
|
bench.end(n);
|
|
break;
|
|
case 'multi-join':
|
|
bench.start();
|
|
for (let i = 0; i < n; i++)
|
|
string = ['...', str, ', ', num, ', ', str, ', ', num, '.'].join('');
|
|
bench.end(n);
|
|
break;
|
|
case 'multi-template':
|
|
bench.start();
|
|
for (let i = 0; i < n; i++)
|
|
string = `...${str}, ${num}, ${str}, ${num}.`;
|
|
bench.end(n);
|
|
break;
|
|
case 'to-string-string':
|
|
bench.start();
|
|
for (let i = 0; i < n; i++)
|
|
string = String(num);
|
|
bench.end(n);
|
|
break;
|
|
case 'to-string-concat':
|
|
bench.start();
|
|
for (let i = 0; i < n; i++)
|
|
string = '' + num;
|
|
bench.end(n);
|
|
break;
|
|
case 'to-string-template':
|
|
bench.start();
|
|
for (let i = 0; i < n; i++)
|
|
string = `${num}`;
|
|
bench.end(n);
|
|
break;
|
|
}
|
|
|
|
return string;
|
|
}
|
|
|