-
Notifications
You must be signed in to change notification settings - Fork 30k
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
lib/internal/util.js#join function is slower than the built-in Array#join #33732
Comments
I suppose it's not too surprising V8 caught up with us. :-) A pull request switching to const { ArrayPrototypeJoin } = primordials; That's |
The benchmark will only be faster for big arrays. Small arrays still profit from the current implementation and that's why it's currently used. |
@bnoordhuis Thank you for your advice! @BridgeAR Thank you for teaching the reason to apply the function! Join string performance check
native/join: 16.52709999680519
util/join: 9.16829900443554
Join number performance check
native/join: 14.551398992538452
util/join: 9.72800000011921 const {performance} = require('perf_hooks')
/* start from lib/internal/util.js #L326 */
// The build-in Array#join is slower in v8 6.0
function join(output, separator) {
let str = '';
if (output.length !== 0) {
const lastIndex = output.length - 1;
for (let i = 0; i < lastIndex; i++) {
// It is faster not to use a template string here
str += output[i];
str += separator;
}
str += output[lastIndex];
}
return str;
}
/* end at lib/internal/util.js #L339 */
const arr1 = (new Array(5)).fill(0).map((v,i) => i % 10 + "")
const arr2 = (new Array(5)).fill(0).map((v,i) => i % 10)
var time1, time2
console.log('Join string performance check')
time1 = performance.now()
for(var i = 0; i < 100000; ++i) arr1.join('@')
time2 = performance.now()
for(var i = 0; i < 100000; ++i) join(arr1, '@')
time3 = performance.now()
console.log('native/join:', time2-time1)
console.log(' util/join:', time3-time2)
console.log('\nJoin number performance check')
time1 = performance.now()
for(var i = 0; i < 100000; ++i) arr2.join('@')
time2 = performance.now()
for(var i = 0; i < 100000; ++i) join(arr2, '@')
time3 = performance.now()
console.log('native/join:', time2-time1)
console.log(' util/join:', time3-time2) |
When I run the below code with v12.16.2 in Win10, the performance of
lib/internal/util.js#join
is about 30 times slower than nativeArray#join
. It looks better to replace to native one.This function is used in
lib/internal/util/inspect.js
in a quick look.I cannot judge an argument
output
is Array or ArrayLike, so I could not fix, sorry.(Add: This would be one of the ways to replace easily ref)
The text was updated successfully, but these errors were encountered: