-
Notifications
You must be signed in to change notification settings - Fork 26.8k
[Arrays] To convert an array-like object to an array, use Array.from #1084
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
Comments
That's a good point - we should be recommending the spread operator here, even though they're equivalent. It might also be worth noting that it could be helpful to avoid the array creation step in some cases, ie: // bad
const bar = [...foo].map(mapper);
// good
const bar = Array.prototype.map.call(foo, mapper); |
Why would the array creation be a problem? IMO |
I agree with you a little bit - but the efficiency of avoiding the extra array reification matters. It's why |
A wise man once told me that the last thing we should care about is performance. |
I might be wrong on this but I just did a speed test on both and the spread test keeps coming on top. const arr = [ ...Array(100000).keys() ];
let test = function (i) { return i + 1; };
let arrFrom = function () {
console.time('arrayFromTest');
Array.from(arr, test);
console.timeEnd('arrayFromTest');
};
let arrSpread = function () {
console.time('arraySpreadTest');
[...arr].map(test);
console.timeEnd('arraySpreadTest');
};
arrFrom();
arrSpread(); What am I doing wrong? |
@rileybracken "running a microbenchmark". Performance can't be usefully measured in that way, since the engine will optimize the mapper function. Try using |
Ah, now results are all over, I think I will just have to believe you 😉. Here it is if you want to try it. http://jsbin.com/garayo/1/edit?js,console |
In 4.3 you say to use the array operator to copy arrays.
Why not do the same for creating arrays 4.4?
Obviously the title would need to change, but I think the syntax is more consistent with 4.3.
The text was updated successfully, but these errors were encountered: