Improve Array#shift performance on v8 > 7.1 (Node 12+) #2115
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Command
Before
Node 10
Node 12
After
Node 10
Node 12
Summary
With this change, the code is now 2.18x times slower when using Node 10 but 24.58x times faster when using Node 12.
We cannot beat the highly optimized code produced by v8 pre 7.1 (Node 10) but the code is now "only" 1.6x times slower when using a recent version of v8/Node.
Please note that the performance on Node 14 is roughly equivalent to Node 12.
Further improvement
The
Array#shift()
(JavaScript) function is also used inString#tr
andString#tr_s
. It might be worth to replace this call by the optimizedshiftNoArg
function? In this case, where should I declare theshiftNoArg
function? inruntime.js
?I also found an optimization for
splice
when the second argument is1
(ie.list.splice(position, 1)
). I found 8 occurrences in the code where the second argument is explicitly 1. When a variable is used as the second argument it might be worth to check if the value is actually 1 to use the optimized code (I think it's only worth if the array is large enough but that means that we need to add an additional check that will slow down the execution).