-
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
Code question #13430
Comments
I think this has to do with allowing the function to be optimized. V8 has some odd optimization behavior regarding |
So spread operator for arguments array is too slower? |
Yes, both the spread operator and Edit: Remove inaccurate benchmark |
@tniessen Are rest arguments still slow on 5.8? I.e. |
@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
|
I tested that function forLoop() {
var args = new Array(arguments.length);
for(var i = 0, len = arguments.length; i < len; i++)
args[i] = arguments[i];
}
function ArrayFrom() {
var args = Array.from(arguments);
}
function ArraySlice() {
var args = Array.prototype.slice.call(arguments);
}
function spreadArray() {
var args = [...arguments];
}
function spreadPush() {
var args = [];
args.push(...arguments);
}
function spreadArg(...args) {
}
var tests = [ forLoop, ArrayFrom, ArraySlice, spreadArray, spreadPush, spreadArg ];
for (var i = 0; i < tests.length; i++){
var test=tests[i], j=0;
console.log(test.name);
console.time(test.name + 0);
for (j = 0; j < 100000; j++){
test();
}
console.timeEnd(test.name + 0);
console.time(test.name+1);
for (j = 0; j < 100000; j++){
test(1);
}
console.timeEnd(test.name + 1);
console.time(test.name+2);
for (j = 0; j < 100000; j++){
test(1, 2);
}
console.timeEnd(test.name + 2);
console.time(test.name + 3);
for (j = 0; j < 100000; j++){
test(1, 2, 3);
}
console.timeEnd(test.name + 3);
console.time(test.name + 4);
for (j = 0; j < 100000; j++){
test(1, 2, 3, 4);
}
console.timeEnd(test.name + 4);
console.time(test.name + 5);
for (j = 0; j < 100000; j++){
test(1, 2, 3, 4, 5);
}
console.timeEnd(test.name + 5);
console.time(test.name + 6);
for (j = 0; j < 100000; j++){
test(1, 2, 3, 4, 5, 6);
}
console.timeEnd(test.name + 6);
console.time(test.name + 7);
for (j = 0; j < 100000; j++){
test(1, 2, 3, 4, 5, 6, 7);
}
console.timeEnd(test.name + 7);
} |
In v8 5.8, rest parameters are significantly faster than other ways to create an array of the arguments. Refs: nodejs#13430
@simonkcleung You have to be careful when benchmarking. For example, in that benchmarking code you gave, the If you add something like FWIW here are example results of what I'm seeing with
|
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. |
Oh, reference for the perf boost for spread operator: https://github.com/davidmarkclements/v8-perf |
In v8 6.0, rest parameters are significantly faster than other ways to create an array of the arguments, even for small numbers. PR-URL: #13472 Refs: #13430 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
In v8 6.0, rest parameters are significantly faster than other ways to create an array of the arguments, even for small numbers. PR-URL: #13472 Refs: #13430 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
Thank you |
Why use this
instead of
Edit by @tniessen: Formatting
The text was updated successfully, but these errors were encountered: