-
Notifications
You must be signed in to change notification settings - Fork 30.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
benchmark: reduce string concatenations #12455
Changes from all commits
cbfa141
3c800ef
c61bdd7
620a87c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's funny we even had a recursive implementation :D There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I am even sorry to retire it) |
||
if (times === 1) return str; | ||
|
||
return str + buildString(str, times - 1); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but there were many other template literals in benchmarks before this edition, including There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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+). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a lot of benchmarks don't run on v4.x or older anyways...I've seen people on IRC |
||
|
||
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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = ` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely not a fan of multiline literals. |
||
return function ${method}(n, buf) { | ||
for (var i = 0; i <= n; i++) | ||
buf.${method}(); | ||
}`; | ||
return (new Function(fnString))(); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', ` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. much nicer |
||
for (var i = 0; i !== ${len}; i++) { | ||
buff.${fn}(i, 0, ${JSON.stringify(noAssert)}); | ||
} | ||
`); | ||
bench.start(); | ||
testFunction(buff); | ||
bench.end(len / 1e6); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW I'm not particularly a fan of this style to avoid the extra spacing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you elaborate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
${
followed by a newline to avoid including spaces due to indentation.