0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-28 14:33:11 +01:00
nodejs/benchmark/es/string-concatenations.js
Liu Jia bb2dd0e90c
benchmark: change iterations in benchmark/es/string-concatenations.js
Increase the number of iterations from `1e3` to `1e6`
to avoid the test performance gap caused by inactive
V8 optimization caused by too few iterations.

Fixes: https://github.com/nodejs/node/issues/50571
PR-URL: https://github.com/nodejs/node/pull/50585
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Debadree Chatterjee <debadree333@gmail.com>
2023-11-09 05:45:33 +00:00

68 lines
1.4 KiB
JavaScript

'use strict';
const common = require('../common.js');
const configs = {
n: [1e6],
mode: [
'multi-concat',
'multi-join',
'multi-template',
'to-string-string',
'to-string-concat',
'to-string-template',
],
};
const bench = common.createBenchmark(main, configs);
function main({ n, 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;
default:
throw new Error(`Unexpected method "${mode}"`);
}
return string;
}