-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
benchmark: reduce string concatenations #12455
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
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) { | ||
|
||
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}`; | ||
|
||
|
||
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 = ` | ||
|
||
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', ` | ||
|
||
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.