2017-04-17 03:07:43 +02:00
|
|
|
'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);
|
|
|
|
|
2017-12-30 03:59:27 +01:00
|
|
|
function main({ n, mode }) {
|
2017-04-17 03:07:43 +02:00
|
|
|
const str = 'abc';
|
|
|
|
const num = 123;
|
|
|
|
|
|
|
|
let string;
|
|
|
|
|
2017-12-30 03:59:27 +01:00
|
|
|
switch (mode) {
|
2017-10-06 18:59:28 +02:00
|
|
|
case '':
|
|
|
|
// Empty string falls through to next line as default, mostly for tests.
|
2017-04-17 03:07:43 +02:00
|
|
|
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;
|
2017-10-06 18:59:28 +02:00
|
|
|
default:
|
2018-01-23 13:18:30 +01:00
|
|
|
throw new Error(`Unexpected method "${mode}"`);
|
2017-04-17 03:07:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|