Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upbenchmark: reduce string concatenations #12455
Conversation
vsemozhetbyt
added
the
benchmark
label
Apr 17, 2017
nodejs-github-bot
added
assert
benchmark
buffer
crypto
fs
http
module
net
V8 Engine
labels
Apr 17, 2017
vsemozhetbyt
removed
assert
buffer
crypto
fs
http
module
net
V8 Engine
labels
Apr 17, 2017
vsemozhetbyt
force-pushed the
vsemozhetbyt:bench-templates
branch
Apr 17, 2017
This comment has been minimized.
This comment has been minimized.
mscdex
reviewed
Apr 17, 2017
'Uint32Array', | ||
'Float32Array', | ||
'Float64Array', | ||
'Uint8ClampedArray', |
This comment has been minimized.
This comment has been minimized.
mscdex
reviewed
Apr 17, 2017
'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.`)); |
This comment has been minimized.
This comment has been minimized.
mscdex
Apr 17, 2017
•
Contributor
Why the odd indentation here and elsewhere? Shouldn't they line up with the beginning of the string?
This comment has been minimized.
This comment has been minimized.
vsemozhetbyt
Apr 17, 2017
Author
Member
I've tried to stress the subordinate state of the next part. I'll try to decrease indentation.
mscdex
reviewed
Apr 17, 2017
`| ${fraction(completedRunsForFile, runsPerFile)} runs ` + | ||
`| ${fraction(completedConfig, scheduledConfig)} configs]` + | ||
`: ${caption} `; | ||
return `[${getTime(diff)}|% ${ |
This comment has been minimized.
This comment has been minimized.
mscdex
Apr 17, 2017
Contributor
FWIW I'm not particularly a fan of this style to avoid the extra spacing.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
mscdex
Apr 17, 2017
Contributor
Using ${
followed by a newline to avoid including spaces due to indentation.
vsemozhetbyt
force-pushed the
vsemozhetbyt:bench-templates
branch
Apr 17, 2017
This comment has been minimized.
This comment has been minimized.
Some indentation was reduced. New CI: https://ci.nodejs.org/job/node-test-pull-request/7435/ |
benjamingr
reviewed
Apr 17, 2017
@@ -54,9 +54,3 @@ function main(conf) { | |||
} | |||
bench.end(n); | |||
} | |||
|
|||
function buildString(str, times) { |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
benjamingr
reviewed
Apr 17, 2017
' buff.' + fn + '(i, 0, ' + JSON.stringify(noAssert) + ');', | ||
'}' | ||
].join('\n')); | ||
var testFunction = new Function('buff', ` |
This comment has been minimized.
This comment has been minimized.
benjamingr
reviewed
Apr 17, 2017
@@ -44,7 +44,7 @@ Benchmark.prototype._parseArgs = function(argv, configs) { | |||
for (const arg of argv) { | |||
const match = arg.match(/^(.+?)=([\s\S]*)$/); | |||
if (!match || !match[1]) { | |||
console.error('bad argument: ' + arg); | |||
console.error(`bad argument: ${arg}`); |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
vsemozhetbyt
Apr 17, 2017
Author
Member
I think this would imply we need inspect()
for arg
(as we do for Error objects), while arg
is a simple string.
benjamingr
approved these changes
Apr 17, 2017
jasnell
reviewed
Apr 17, 2017
@@ -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}`; |
This comment has been minimized.
This comment has been minimized.
jasnell
Apr 17, 2017
Member
just a reminder that this will make it impossible to run these benchmarks to compare against anything older than 4.0.0. That's not an objection, by any means.
This comment has been minimized.
This comment has been minimized.
vsemozhetbyt
Apr 17, 2017
•
Author
Member
Yes, but there were many other template literals in benchmarks before this edition, including common.js
.
This comment has been minimized.
This comment has been minimized.
mscdex
Apr 17, 2017
•
Contributor
At this point I don't think it really matters. I can't see anyone wanting to run the benchmarks for v0.10 or v0.12 anymore, especially since it wouldn't really do any good (it's not as if the V8 team can/will revert to v0.10/v0.12-era V8 code if there is a performance issue in node v4.x+).
This comment has been minimized.
This comment has been minimized.
jasnell
Apr 17, 2017
Member
Yep, as I said, it's not an objection. Just noting the change. We may want to let folks know just so that they're aware.
This comment has been minimized.
This comment has been minimized.
joyeecheung
Apr 21, 2017
Member
I think a lot of benchmarks don't run on v4.x or older anyways...I've seen people on IRC
asking why some benchmark doesn't run and turns out they are using v4.x to run it
vsemozhetbyt commentedApr 17, 2017
Checklist
Affected core subsystem(s)
benchmark
I. Presupposition
String concatenations usually increase syntax noise and add confusion (a reader often has to check if string concatenation or number addition is intended). Moreover, it seems this method is not optimal performance-wise.
II. Cases
This PR aims two cases of string concatenation:
'a' + b + 'c'
);String
('' + notString
). This approach, while idiomatic, is not most declarative.III. Performance
The added benchmark compares these variants:
'a' + b + 'c'
vs['a', b, 'c'].join('')
vs`a${b}c`
String(notString)
vs'' + notString
vs`${notString}`
Results with Node.js 8.0.0 rc:
IV. Commits
Reduce string concatenations: replace concatenations with template literals, rewrap,
String.prototype.repeat()
,String()
. Some replacements do not add indisputable readability gain, but I tried to be consistent.Add a benchmark for string concatenations vs counterparts.
This commit is added here to avoid PR race condition and merge conflicts. It fixes an URL in
_http-benchmarkers.js
(removes duplicate hash symbol).This commit is added here to avoid PR race condition and merge conflicts. It fixes an evident typo in
_http-benchmarkers.js
:Error()
constructor has only one parameter, whilecallback()
has 5 (compare a previous call andcallback()
signature).All changed files were minimally tested. CI for linting and new tests concerning benchmarks will be launched.