You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I think this has to do with allowing the function to be optimized. V8 has some odd optimization behavior regarding arguments -- see Optimization Killers.
@simonkcleung You have to be careful when benchmarking. For example, in that benchmarking code you gave, the spreadArg() function will optimized away because it contains no code, so you end up benchmarking how fast V8 can execute an empty for-loop.
If you add something like return args[0] + 1 to each function, that would be more representative of performance for each solution. It's also good to execute each different benchmark in isolation because I've seen the execution of one benchmarked function affect the results of further benchmarked functions (e.g. executing ArrayFrom() benchmarks after forLoop()). When I make the necessary changes, I see that forLoop() is actually the fastest for <= 4 arguments. Anything more than that, rest params do seem to be somewhat faster.
FWIW here are example results of what I'm seeing with spreadArg() and forLoop() on master (with iterations bumped up to 1e8):
Spread operator should get a significant performance bump when Node.js 8.3.0 (with V8 6.0) comes out (probably this week).
Closing as this is not really a Node.js issue. (The spread operator is implemented by V8.) Feel free to comment or (if GitHub lets you) re-open if you think that's misguided of me.
Activity
not-an-aardvark commentedon Jun 3, 2017
I think this has to do with allowing the function to be optimized. V8 has some odd optimization behavior regarding
arguments-- see Optimization Killers.gamelaster commentedon Jun 3, 2017
So spread operator for arguments array is too slower?
tniessen commentedon Jun 3, 2017
Yes, both the spread operator and
Array.from(arguments)are currently slower than a good old loop.Edit: Remove inaccurate benchmark
ChALkeR commentedon Jun 3, 2017
@tniessen Are rest arguments still slow on 5.8? I.e.
function(...args) {.tniessen commentedon Jun 3, 2017
@ChALkeR Sorry, the last benchmark was inaccurate thanks to some v8 optimizations.
Edit: Correct benchmark.
Non-strict mode, normal
Non-strict mode, apply
Strict mode, normal
Strict mode, apply
simonkcleung commentedon Jun 4, 2017
I tested that
function test(...args) {}is fastest.Code:
net: use rest parameters instead of arguments
mscdex commentedon Jun 5, 2017
@simonkcleung You have to be careful when benchmarking. For example, in that benchmarking code you gave, the
spreadArg()function will optimized away because it contains no code, so you end up benchmarking how fast V8 can execute an empty for-loop.If you add something like
return args[0] + 1to each function, that would be more representative of performance for each solution. It's also good to execute each different benchmark in isolation because I've seen the execution of one benchmarked function affect the results of further benchmarked functions (e.g. executingArrayFrom()benchmarks afterforLoop()). When I make the necessary changes, I see thatforLoop()is actually the fastest for <= 4 arguments. Anything more than that, rest params do seem to be somewhat faster.FWIW here are example results of what I'm seeing with
spreadArg()andforLoop()on master (with iterations bumped up to1e8):Trott commentedon Aug 7, 2017
Spread operator should get a significant performance bump when Node.js 8.3.0 (with V8 6.0) comes out (probably this week).
Closing as this is not really a Node.js issue. (The spread operator is implemented by V8.) Feel free to comment or (if GitHub lets you) re-open if you think that's misguided of me.
Trott commentedon Aug 7, 2017
Oh, reference for the perf boost for spread operator: https://github.com/davidmarkclements/v8-perf
net: use rest parameters instead of arguments
net: use rest parameters instead of arguments
fsx950223 commentedon Oct 13, 2017
Thank you