Closed
Description
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 native Array#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)
Join string performance check
native/join: 5.081699997186661 ms
util/join: 146.29249899089336 ms
Join number performance check
native/join: 4.920000001788139 ms
util/join: 155.2229000031948 ms
const {performance} = require('perf_hooks')
/* start copy 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 copy lib/internal/util.js #L339 */
const arr1 = (new Array(1000000)).fill(0).map((v,i) => i % 10 + "")
const arr2 = (new Array(1000000)).fill(0).map((v,i) => i % 10)
var time1, time2
console.log('Join string performance check')
time1 = performance.now()
arr1.join('@')
time2 = performance.now()
join(arr1, '@')
time3 = performance.now()
console.log('native/join:', time2-time1, 'ms')
console.log(' util/join:', time3-time2, 'ms')
console.log('\nJoin number performance check')
time1 = performance.now()
arr2.join('@')
time2 = performance.now()
join(arr2, '@')
time3 = performance.now()
console.log('native/join:', time2-time1, 'ms')
console.log(' util/join:', time3-time2, 'ms')
Metadata
Metadata
Assignees
Labels
No labels