Permalink
Browse files

benchmark: reduce string concatenations

PR-URL: #12455
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information...
vsemozhetbyt committed Apr 17, 2017
1 parent bbbb1f6 commit 22aa3d48997f1fc31e9c9cc08ec76a5436c93fe3
Showing with 168 additions and 162 deletions.
  1. +10 −8 benchmark/_benchmark_progress.js
  2. +11 −9 benchmark/_http-benchmarkers.js
  3. +11 −2 benchmark/assert/deepequal-typedarrays.js
  4. +1 −1 benchmark/buffers/buffer-base64-decode-wrapped.js
  5. +1 −7 benchmark/buffers/buffer-bytelength.js
  6. +6 −6 benchmark/buffers/buffer-read.js
  7. +5 −5 benchmark/buffers/buffer-swap.js
  8. +11 −11 benchmark/buffers/buffer-write.js
  9. +1 −1 benchmark/buffers/dataview-set.js
  10. +2 −2 benchmark/common.js
  11. +3 −3 benchmark/compare.js
  12. +1 −1 benchmark/crypto/cipher-stream.js
  13. +1 −1 benchmark/crypto/hash-stream-creation.js
  14. +1 −1 benchmark/crypto/hash-stream-throughput.js
  15. +4 −4 benchmark/crypto/rsa-encrypt-decrypt-throughput.js
  16. +4 −4 benchmark/crypto/rsa-sign-verify-throughput.js
  17. +33 −33 benchmark/es/map-bench.js
  18. +1 −1 benchmark/fs/bench-realpath.js
  19. +1 −1 benchmark/fs/bench-realpathSync.js
  20. +1 −1 benchmark/http/_chunky_http_client.js
  21. +1 −1 benchmark/http/cluster.js
  22. +2 −2 benchmark/http/http_server_for_chunky_client.js
  23. +1 −2 benchmark/http/simple.js
  24. +4 −4 benchmark/misc/console.js
  25. +4 −4 benchmark/misc/v8-bench.js
  26. +7 −7 benchmark/module/module-loader.js
  27. +1 −1 benchmark/net/net-c2s-cork.js
  28. +1 −1 benchmark/net/net-c2s.js
  29. +1 −1 benchmark/net/net-pipe.js
  30. +1 −1 benchmark/net/net-s2c.js
  31. +1 −1 benchmark/net/tcp-raw-c2s.js
  32. +1 −1 benchmark/net/tcp-raw-pipe.js
  33. +1 −1 benchmark/net/tcp-raw-s2c.js
  34. +1 −1 benchmark/path/basename-posix.js
  35. +1 −1 benchmark/path/basename-win32.js
  36. +1 −1 benchmark/path/dirname-posix.js
  37. +1 −1 benchmark/path/dirname-win32.js
  38. +1 −1 benchmark/path/extname-posix.js
  39. +1 −1 benchmark/path/extname-win32.js
  40. +1 −1 benchmark/path/format-posix.js
  41. +1 −1 benchmark/path/format-win32.js
  42. +1 −1 benchmark/path/isAbsolute-posix.js
  43. +1 −1 benchmark/path/isAbsolute-win32.js
  44. +1 −1 benchmark/path/join-posix.js
  45. +1 −1 benchmark/path/join-win32.js
  46. +1 −1 benchmark/path/makeLong-win32.js
  47. +1 −1 benchmark/path/normalize-posix.js
  48. +1 −1 benchmark/path/normalize-win32.js
  49. +1 −1 benchmark/path/parse-posix.js
  50. +1 −1 benchmark/path/parse-win32.js
  51. +1 −1 benchmark/path/relative-posix.js
  52. +1 −1 benchmark/path/relative-win32.js
  53. +1 −1 benchmark/path/resolve-posix.js
  54. +1 −1 benchmark/path/resolve-win32.js
  55. +1 −1 benchmark/run.js
  56. +1 −1 benchmark/scatter.js
  57. +1 −1 benchmark/timers/timers-cancel-unpooled.js
  58. +1 −1 benchmark/timers/timers-insert-unpooled.js
  59. +3 −3 benchmark/tls/throughput.js
  60. +3 −3 benchmark/tls/tls-connect.js
  61. +2 −2 benchmark/url/legacy-vs-whatwg-url-get-prop.js
@@ -3,12 +3,13 @@
const readline = require('readline');
function pad(input, minLength, fill) {
var result = input + '';
return fill.repeat(Math.max(0, minLength - result.length)) + result;
var result = String(input);
var padding = fill.repeat(Math.max(0, minLength - result.length));
return `${padding}${result}`;
}
function fraction(numerator, denominator) {
const fdenominator = denominator + '';
const fdenominator = String(denominator);
const fnumerator = pad(numerator, fdenominator.length, ' ');
return `${fnumerator}/${fdenominator}`;
}
@@ -100,11 +101,12 @@ class BenchmarkProgress {
const percent = pad(Math.floor(completedRate * 100), 3, ' ');
const caption = finished ? 'Done\n' : this.currentFile;
return `[${getTime(diff)}|% ${percent}` +
`| ${fraction(completedFiles, scheduledFiles)} files ` +
`| ${fraction(completedRunsForFile, runsPerFile)} runs ` +
`| ${fraction(completedConfig, scheduledConfig)} configs]` +
`: ${caption} `;
return `[${getTime(diff)}|% ${
percent}| ${
fraction(completedFiles, scheduledFiles)} files | ${
fraction(completedRunsForFile, runsPerFile)} runs | ${
fraction(completedConfig, scheduledConfig)} configs]: ${
caption} `;
}
updateProgress(finished) {
@@ -4,6 +4,9 @@ const child_process = require('child_process');
const path = require('path');
const fs = require('fs');
const requirementsURL =
'https://github.com/nodejs/node/blob/master/doc/guides/writing-and-running-benchmarks.md##http-benchmark-requirements';
// The port used by servers and wrk
exports.PORT = process.env.PORT || 12346;
@@ -133,20 +136,19 @@ exports.run = function(options, callback) {
benchmarker: exports.default_http_benchmarker
}, options);
if (!options.benchmarker) {
callback(new Error('Could not locate required http benchmarker. See ' +
'https://github.com/nodejs/node/blob/master/doc/guides/writing-and-running-benchmarks.md##http-benchmark-requirements ' +
'for further instructions.'));
callback(new Error(`Could not locate required http benchmarker. See ${
requirementsURL} for further instructions.`));
return;
}
const benchmarker = benchmarkers[options.benchmarker];
if (!benchmarker) {
callback(new Error(`Requested benchmarker '${options.benchmarker}' is ` +
'not supported'));
callback(new Error(`Requested benchmarker '${
options.benchmarker}' is not supported`));
return;
}
if (!benchmarker.present) {
callback(new Error(`Requested benchmarker '${options.benchmarker}' is ` +
'not installed'));
callback(new Error(`Requested benchmarker '${
options.benchmarker}' is not installed`));
return;
}
@@ -172,8 +174,8 @@ exports.run = function(options, callback) {
const result = benchmarker.processResults(stdout);
if (result === undefined) {
callback(new Error(`${options.benchmarker} produced strange output: ` +
stdout, code));
callback(new Error(
`${options.benchmarker} produced strange output: ${stdout}`, code));
return;
}
@@ -2,8 +2,17 @@
const common = require('../common.js');
const assert = require('assert');
const bench = common.createBenchmark(main, {
type: ('Int8Array Uint8Array Int16Array Uint16Array Int32Array Uint32Array ' +
'Float32Array Float64Array Uint8ClampedArray').split(' '),
type: [
'Int8Array',
'Uint8Array',
'Int16Array',
'Uint16Array',
'Int32Array',
'Uint32Array',
'Float32Array',
'Float64Array',
'Uint8ClampedArray',
],
n: [1],
method: ['strict', 'nonstrict'],
len: [1e6]
@@ -12,7 +12,7 @@ function main(conf) {
const linesCount = 8 << 16;
const bytesCount = charsPerLine * linesCount / 4 * 3;
const line = 'abcd'.repeat(charsPerLine / 4) + '\n';
const line = `${'abcd'.repeat(charsPerLine / 4)}\n`;
const data = line.repeat(linesCount);
// eslint-disable-next-line no-unescaped-regexp-dot
data.match(/./); // Flatten the string
@@ -28,7 +28,7 @@ function main(conf) {
} else {
for (var string of chars) {
// Strings must be built differently, depending on encoding
var data = buildString(string, len);
var data = string.repeat(len);
if (encoding === 'utf8') {
strings.push(data);
} else if (encoding === 'base64') {
@@ -54,9 +54,3 @@ function main(conf) {
}
bench.end(n);
}
function buildString(str, times) {
if (times === 1) return str;
return str + buildString(str, times - 1);
}
@@ -30,14 +30,14 @@ function main(conf) {
var len = +conf.millions * 1e6;
var clazz = conf.buf === 'fast' ? Buffer : require('buffer').SlowBuffer;
var buff = new clazz(8);
var fn = 'read' + conf.type;
var fn = `read${conf.type}`;
buff.writeDoubleLE(0, 0, noAssert);
var testFunction = new Function('buff', [
'for (var i = 0; i !== ' + len + '; i++) {',
' buff.' + fn + '(0, ' + JSON.stringify(noAssert) + ');',
'}'
].join('\n'));
var testFunction = new Function('buff', `
for (var i = 0; i !== ${len}; i++) {
buff.${fn}(0, ${JSON.stringify(noAssert)});
}
`);
bench.start();
testFunction(buff);
bench.end(len / 1e6);
@@ -64,11 +64,11 @@ function createBuffer(len, aligned) {
}
function genMethod(method) {
const fnString =
'return function ' + method + '(n, buf) {' +
' for (var i = 0; i <= n; i++)' +
' buf.' + method + '();' +
'}';
const fnString = `
return function ${method}(n, buf) {
for (var i = 0; i <= n; i++)
buf.${method}();
}`;
return (new Function(fnString))();
}
@@ -50,7 +50,7 @@ function main(conf) {
var len = +conf.millions * 1e6;
var clazz = conf.buf === 'fast' ? Buffer : require('buffer').SlowBuffer;
var buff = new clazz(8);
var fn = 'write' + conf.type;
var fn = `write${conf.type}`;
if (fn.match(/Int/))
benchInt(buff, fn, len, noAssert);
@@ -60,22 +60,22 @@ function main(conf) {
function benchInt(buff, fn, len, noAssert) {
var m = mod[fn];
var testFunction = new Function('buff', [
'for (var i = 0; i !== ' + len + '; i++) {',
' buff.' + fn + '(i & ' + m + ', 0, ' + JSON.stringify(noAssert) + ');',
'}'
].join('\n'));
var testFunction = new Function('buff', `
for (var i = 0; i !== ${len}; i++) {
buff.${fn}(i & ${m}, 0, ${JSON.stringify(noAssert)});
}
`);
bench.start();
testFunction(buff);
bench.end(len / 1e6);
}
function benchFloat(buff, fn, len, noAssert) {
var testFunction = new Function('buff', [
'for (var i = 0; i !== ' + len + '; i++) {',
' buff.' + fn + '(i, 0, ' + JSON.stringify(noAssert) + ');',
'}'
].join('\n'));
var testFunction = new Function('buff', `
for (var i = 0; i !== ${len}; i++) {
buff.${fn}(i, 0, ${JSON.stringify(noAssert)});
}
`);
bench.start();
testFunction(buff);
bench.end(len / 1e6);
@@ -44,7 +44,7 @@ function main(conf) {
var ab = new ArrayBuffer(8);
var dv = new DataView(ab, 0, 8);
var le = /LE$/.test(conf.type);
var fn = 'set' + conf.type.replace(/[LB]E$/, '');
var fn = `set${conf.type.replace(/[LB]E$/, '')}`;
if (/int/i.test(fn))
benchInt(dv, fn, len, le);
View
@@ -44,7 +44,7 @@ Benchmark.prototype._parseArgs = function(argv, configs) {
for (const arg of argv) {
const match = arg.match(/^(.+?)=([\s\S]*)$/);
if (!match) {
console.error('bad argument: ' + arg);
console.error(`bad argument: ${arg}`);
process.exit(1);
}
const config = match[1];
@@ -206,7 +206,7 @@ function formatResult(data) {
// Construct configuration string, " A=a, B=b, ..."
let conf = '';
for (const key of Object.keys(data.conf)) {
conf += ' ' + key + '=' + JSON.stringify(data.conf[key]);
conf += ` ${key}=${JSON.stringify(data.conf[key])}`;
}
var rate = data.rate.toString().split('.');
View
@@ -79,14 +79,14 @@ if (showProgress) {
// Construct configuration string, " A=a, B=b, ..."
let conf = '';
for (const key of Object.keys(data.conf)) {
conf += ' ' + key + '=' + JSON.stringify(data.conf[key]);
conf += ` ${key}=${JSON.stringify(data.conf[key])}`;
}
conf = conf.slice(1);
// Escape quotes (") for correct csv formatting
conf = conf.replace(/"/g, '""');
console.log(`"${job.binary}", "${job.filename}", "${conf}", ` +
`${data.rate}, ${data.time}`);
console.log(`"${job.binary}", "${job.filename}", "${conf}", ${
data.rate}, ${data.time}`);
if (showProgress) {
// One item in the subqueue has been completed.
progress.completeConfig(data);
@@ -51,7 +51,7 @@ function main(conf) {
message = Buffer.alloc(conf.len, 'b');
break;
default:
throw new Error('unknown message type: ' + conf.type);
throw new Error(`unknown message type: ${conf.type}`);
}
var fn = api === 'stream' ? streamWrite : legacyWrite;
@@ -36,7 +36,7 @@ function main(conf) {
message = Buffer.alloc(conf.len, 'b');
break;
default:
throw new Error('unknown message type: ' + conf.type);
throw new Error(`unknown message type: ${conf.type}`);
}
var fn = api === 'stream' ? streamWrite : legacyWrite;
@@ -35,7 +35,7 @@ function main(conf) {
message = Buffer.alloc(conf.len, 'b');
break;
default:
throw new Error('unknown message type: ' + conf.type);
throw new Error(`unknown message type: ${conf.type}`);
}
var fn = api === 'stream' ? streamWrite : legacyWrite;
@@ -10,10 +10,10 @@ var RSA_PublicPem = {};
var RSA_PrivatePem = {};
keylen_list.forEach(function(key) {
RSA_PublicPem[key] = fs.readFileSync(fixtures_keydir +
'/rsa_public_' + key + '.pem');
RSA_PrivatePem[key] = fs.readFileSync(fixtures_keydir +
'/rsa_private_' + key + '.pem');
RSA_PublicPem[key] =
fs.readFileSync(`${fixtures_keydir}/rsa_public_${key}.pem`);
RSA_PrivatePem[key] =
fs.readFileSync(`${fixtures_keydir}/rsa_private_${key}.pem`);
});
var bench = common.createBenchmark(main, {
@@ -10,10 +10,10 @@ var RSA_PublicPem = {};
var RSA_PrivatePem = {};
keylen_list.forEach(function(key) {
RSA_PublicPem[key] = fs.readFileSync(fixtures_keydir +
'/rsa_public_' + key + '.pem');
RSA_PrivatePem[key] = fs.readFileSync(fixtures_keydir +
'/rsa_private_' + key + '.pem');
RSA_PublicPem[key] =
fs.readFileSync(`${fixtures_keydir}/rsa_public_${key}.pem`);
RSA_PrivatePem[key] =
fs.readFileSync(`${fixtures_keydir}/rsa_private_${key}.pem`);
});
var bench = common.createBenchmark(main, {
Oops, something went wrong.

0 comments on commit 22aa3d4

Please sign in to comment.