Skip to content

Commit 22aa3d4

Browse files
committed
benchmark: reduce string concatenations
PR-URL: #12455 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent bbbb1f6 commit 22aa3d4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+168
-162
lines changed

benchmark/_benchmark_progress.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
const readline = require('readline');
44

55
function pad(input, minLength, fill) {
6-
var result = input + '';
7-
return fill.repeat(Math.max(0, minLength - result.length)) + result;
6+
var result = String(input);
7+
var padding = fill.repeat(Math.max(0, minLength - result.length));
8+
return `${padding}${result}`;
89
}
910

1011
function fraction(numerator, denominator) {
11-
const fdenominator = denominator + '';
12+
const fdenominator = String(denominator);
1213
const fnumerator = pad(numerator, fdenominator.length, ' ');
1314
return `${fnumerator}/${fdenominator}`;
1415
}
@@ -100,11 +101,12 @@ class BenchmarkProgress {
100101
const percent = pad(Math.floor(completedRate * 100), 3, ' ');
101102

102103
const caption = finished ? 'Done\n' : this.currentFile;
103-
return `[${getTime(diff)}|% ${percent}` +
104-
`| ${fraction(completedFiles, scheduledFiles)} files ` +
105-
`| ${fraction(completedRunsForFile, runsPerFile)} runs ` +
106-
`| ${fraction(completedConfig, scheduledConfig)} configs]` +
107-
`: ${caption} `;
104+
return `[${getTime(diff)}|% ${
105+
percent}| ${
106+
fraction(completedFiles, scheduledFiles)} files | ${
107+
fraction(completedRunsForFile, runsPerFile)} runs | ${
108+
fraction(completedConfig, scheduledConfig)} configs]: ${
109+
caption} `;
108110
}
109111

110112
updateProgress(finished) {

benchmark/_http-benchmarkers.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ const child_process = require('child_process');
44
const path = require('path');
55
const fs = require('fs');
66

7+
const requirementsURL =
8+
'https://github.com/nodejs/node/blob/master/doc/guides/writing-and-running-benchmarks.md##http-benchmark-requirements';
9+
710
// The port used by servers and wrk
811
exports.PORT = process.env.PORT || 12346;
912

@@ -133,20 +136,19 @@ exports.run = function(options, callback) {
133136
benchmarker: exports.default_http_benchmarker
134137
}, options);
135138
if (!options.benchmarker) {
136-
callback(new Error('Could not locate required http benchmarker. See ' +
137-
'https://github.com/nodejs/node/blob/master/doc/guides/writing-and-running-benchmarks.md##http-benchmark-requirements ' +
138-
'for further instructions.'));
139+
callback(new Error(`Could not locate required http benchmarker. See ${
140+
requirementsURL} for further instructions.`));
139141
return;
140142
}
141143
const benchmarker = benchmarkers[options.benchmarker];
142144
if (!benchmarker) {
143-
callback(new Error(`Requested benchmarker '${options.benchmarker}' is ` +
144-
'not supported'));
145+
callback(new Error(`Requested benchmarker '${
146+
options.benchmarker}' is not supported`));
145147
return;
146148
}
147149
if (!benchmarker.present) {
148-
callback(new Error(`Requested benchmarker '${options.benchmarker}' is ` +
149-
'not installed'));
150+
callback(new Error(`Requested benchmarker '${
151+
options.benchmarker}' is not installed`));
150152
return;
151153
}
152154

@@ -172,8 +174,8 @@ exports.run = function(options, callback) {
172174

173175
const result = benchmarker.processResults(stdout);
174176
if (result === undefined) {
175-
callback(new Error(`${options.benchmarker} produced strange output: ` +
176-
stdout, code));
177+
callback(new Error(
178+
`${options.benchmarker} produced strange output: ${stdout}`, code));
177179
return;
178180
}
179181

benchmark/assert/deepequal-typedarrays.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22
const common = require('../common.js');
33
const assert = require('assert');
44
const bench = common.createBenchmark(main, {
5-
type: ('Int8Array Uint8Array Int16Array Uint16Array Int32Array Uint32Array ' +
6-
'Float32Array Float64Array Uint8ClampedArray').split(' '),
5+
type: [
6+
'Int8Array',
7+
'Uint8Array',
8+
'Int16Array',
9+
'Uint16Array',
10+
'Int32Array',
11+
'Uint32Array',
12+
'Float32Array',
13+
'Float64Array',
14+
'Uint8ClampedArray',
15+
],
716
n: [1],
817
method: ['strict', 'nonstrict'],
918
len: [1e6]

benchmark/buffers/buffer-base64-decode-wrapped.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function main(conf) {
1212
const linesCount = 8 << 16;
1313
const bytesCount = charsPerLine * linesCount / 4 * 3;
1414

15-
const line = 'abcd'.repeat(charsPerLine / 4) + '\n';
15+
const line = `${'abcd'.repeat(charsPerLine / 4)}\n`;
1616
const data = line.repeat(linesCount);
1717
// eslint-disable-next-line no-unescaped-regexp-dot
1818
data.match(/./); // Flatten the string

benchmark/buffers/buffer-bytelength.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function main(conf) {
2828
} else {
2929
for (var string of chars) {
3030
// Strings must be built differently, depending on encoding
31-
var data = buildString(string, len);
31+
var data = string.repeat(len);
3232
if (encoding === 'utf8') {
3333
strings.push(data);
3434
} else if (encoding === 'base64') {
@@ -54,9 +54,3 @@ function main(conf) {
5454
}
5555
bench.end(n);
5656
}
57-
58-
function buildString(str, times) {
59-
if (times === 1) return str;
60-
61-
return str + buildString(str, times - 1);
62-
}

benchmark/buffers/buffer-read.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ function main(conf) {
3030
var len = +conf.millions * 1e6;
3131
var clazz = conf.buf === 'fast' ? Buffer : require('buffer').SlowBuffer;
3232
var buff = new clazz(8);
33-
var fn = 'read' + conf.type;
33+
var fn = `read${conf.type}`;
3434

3535
buff.writeDoubleLE(0, 0, noAssert);
36-
var testFunction = new Function('buff', [
37-
'for (var i = 0; i !== ' + len + '; i++) {',
38-
' buff.' + fn + '(0, ' + JSON.stringify(noAssert) + ');',
39-
'}'
40-
].join('\n'));
36+
var testFunction = new Function('buff', `
37+
for (var i = 0; i !== ${len}; i++) {
38+
buff.${fn}(0, ${JSON.stringify(noAssert)});
39+
}
40+
`);
4141
bench.start();
4242
testFunction(buff);
4343
bench.end(len / 1e6);

benchmark/buffers/buffer-swap.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ function createBuffer(len, aligned) {
6464
}
6565

6666
function genMethod(method) {
67-
const fnString =
68-
'return function ' + method + '(n, buf) {' +
69-
' for (var i = 0; i <= n; i++)' +
70-
' buf.' + method + '();' +
71-
'}';
67+
const fnString = `
68+
return function ${method}(n, buf) {
69+
for (var i = 0; i <= n; i++)
70+
buf.${method}();
71+
}`;
7272
return (new Function(fnString))();
7373
}
7474

benchmark/buffers/buffer-write.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function main(conf) {
5050
var len = +conf.millions * 1e6;
5151
var clazz = conf.buf === 'fast' ? Buffer : require('buffer').SlowBuffer;
5252
var buff = new clazz(8);
53-
var fn = 'write' + conf.type;
53+
var fn = `write${conf.type}`;
5454

5555
if (fn.match(/Int/))
5656
benchInt(buff, fn, len, noAssert);
@@ -60,22 +60,22 @@ function main(conf) {
6060

6161
function benchInt(buff, fn, len, noAssert) {
6262
var m = mod[fn];
63-
var testFunction = new Function('buff', [
64-
'for (var i = 0; i !== ' + len + '; i++) {',
65-
' buff.' + fn + '(i & ' + m + ', 0, ' + JSON.stringify(noAssert) + ');',
66-
'}'
67-
].join('\n'));
63+
var testFunction = new Function('buff', `
64+
for (var i = 0; i !== ${len}; i++) {
65+
buff.${fn}(i & ${m}, 0, ${JSON.stringify(noAssert)});
66+
}
67+
`);
6868
bench.start();
6969
testFunction(buff);
7070
bench.end(len / 1e6);
7171
}
7272

7373
function benchFloat(buff, fn, len, noAssert) {
74-
var testFunction = new Function('buff', [
75-
'for (var i = 0; i !== ' + len + '; i++) {',
76-
' buff.' + fn + '(i, 0, ' + JSON.stringify(noAssert) + ');',
77-
'}'
78-
].join('\n'));
74+
var testFunction = new Function('buff', `
75+
for (var i = 0; i !== ${len}; i++) {
76+
buff.${fn}(i, 0, ${JSON.stringify(noAssert)});
77+
}
78+
`);
7979
bench.start();
8080
testFunction(buff);
8181
bench.end(len / 1e6);

benchmark/buffers/dataview-set.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function main(conf) {
4444
var ab = new ArrayBuffer(8);
4545
var dv = new DataView(ab, 0, 8);
4646
var le = /LE$/.test(conf.type);
47-
var fn = 'set' + conf.type.replace(/[LB]E$/, '');
47+
var fn = `set${conf.type.replace(/[LB]E$/, '')}`;
4848

4949
if (/int/i.test(fn))
5050
benchInt(dv, fn, len, le);

benchmark/common.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Benchmark.prototype._parseArgs = function(argv, configs) {
4444
for (const arg of argv) {
4545
const match = arg.match(/^(.+?)=([\s\S]*)$/);
4646
if (!match) {
47-
console.error('bad argument: ' + arg);
47+
console.error(`bad argument: ${arg}`);
4848
process.exit(1);
4949
}
5050
const config = match[1];
@@ -206,7 +206,7 @@ function formatResult(data) {
206206
// Construct configuration string, " A=a, B=b, ..."
207207
let conf = '';
208208
for (const key of Object.keys(data.conf)) {
209-
conf += ' ' + key + '=' + JSON.stringify(data.conf[key]);
209+
conf += ` ${key}=${JSON.stringify(data.conf[key])}`;
210210
}
211211

212212
var rate = data.rate.toString().split('.');

benchmark/compare.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ if (showProgress) {
7979
// Construct configuration string, " A=a, B=b, ..."
8080
let conf = '';
8181
for (const key of Object.keys(data.conf)) {
82-
conf += ' ' + key + '=' + JSON.stringify(data.conf[key]);
82+
conf += ` ${key}=${JSON.stringify(data.conf[key])}`;
8383
}
8484
conf = conf.slice(1);
8585
// Escape quotes (") for correct csv formatting
8686
conf = conf.replace(/"/g, '""');
8787

88-
console.log(`"${job.binary}", "${job.filename}", "${conf}", ` +
89-
`${data.rate}, ${data.time}`);
88+
console.log(`"${job.binary}", "${job.filename}", "${conf}", ${
89+
data.rate}, ${data.time}`);
9090
if (showProgress) {
9191
// One item in the subqueue has been completed.
9292
progress.completeConfig(data);

benchmark/crypto/cipher-stream.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function main(conf) {
5151
message = Buffer.alloc(conf.len, 'b');
5252
break;
5353
default:
54-
throw new Error('unknown message type: ' + conf.type);
54+
throw new Error(`unknown message type: ${conf.type}`);
5555
}
5656

5757
var fn = api === 'stream' ? streamWrite : legacyWrite;

benchmark/crypto/hash-stream-creation.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function main(conf) {
3636
message = Buffer.alloc(conf.len, 'b');
3737
break;
3838
default:
39-
throw new Error('unknown message type: ' + conf.type);
39+
throw new Error(`unknown message type: ${conf.type}`);
4040
}
4141

4242
var fn = api === 'stream' ? streamWrite : legacyWrite;

benchmark/crypto/hash-stream-throughput.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function main(conf) {
3535
message = Buffer.alloc(conf.len, 'b');
3636
break;
3737
default:
38-
throw new Error('unknown message type: ' + conf.type);
38+
throw new Error(`unknown message type: ${conf.type}`);
3939
}
4040

4141
var fn = api === 'stream' ? streamWrite : legacyWrite;

benchmark/crypto/rsa-encrypt-decrypt-throughput.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ var RSA_PublicPem = {};
1010
var RSA_PrivatePem = {};
1111

1212
keylen_list.forEach(function(key) {
13-
RSA_PublicPem[key] = fs.readFileSync(fixtures_keydir +
14-
'/rsa_public_' + key + '.pem');
15-
RSA_PrivatePem[key] = fs.readFileSync(fixtures_keydir +
16-
'/rsa_private_' + key + '.pem');
13+
RSA_PublicPem[key] =
14+
fs.readFileSync(`${fixtures_keydir}/rsa_public_${key}.pem`);
15+
RSA_PrivatePem[key] =
16+
fs.readFileSync(`${fixtures_keydir}/rsa_private_${key}.pem`);
1717
});
1818

1919
var bench = common.createBenchmark(main, {

benchmark/crypto/rsa-sign-verify-throughput.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ var RSA_PublicPem = {};
1010
var RSA_PrivatePem = {};
1111

1212
keylen_list.forEach(function(key) {
13-
RSA_PublicPem[key] = fs.readFileSync(fixtures_keydir +
14-
'/rsa_public_' + key + '.pem');
15-
RSA_PrivatePem[key] = fs.readFileSync(fixtures_keydir +
16-
'/rsa_private_' + key + '.pem');
13+
RSA_PublicPem[key] =
14+
fs.readFileSync(`${fixtures_keydir}/rsa_public_${key}.pem`);
15+
RSA_PrivatePem[key] =
16+
fs.readFileSync(`${fixtures_keydir}/rsa_private_${key}.pem`);
1717
});
1818

1919
var bench = common.createBenchmark(main, {

0 commit comments

Comments
 (0)