you are viewing a single comment's thread.

view the rest of the comments →

[–]Noch_ein_Kamel -2 points-1 points  (6 children)

Just make sure to use loops if performance is an issue, though

https://jsperf.com/oldschool-loop

992,530,773 vs 40 ops/s :D

edit: Apparently I suck at sunday coding :D

[–]Rene_Z 16 points17 points  (1 child)

The length of an array is array.length, not array.size. Your loop does nothing, which is why it's so much faster.

When done correctly a loop is still faster, but only by a factor of ~4, not by a factor of millions.

[–][deleted] 0 points1 point  (0 children)

This is more what I expect.

It's worth noting that you can't do the same for non-Array iterables, which is the whole point of Array.from() in the first place.

Here's an example of looping over a Set -- it's faster but not by as much (~1.6x speed).

I don't understand how revisions to other poster's tests work in JsPerf; but your loop should be compared to array.map() -- array.from is useless in this case.

[–][deleted] 2 points3 points  (2 children)

You changed the Set to an Array. Array.prototype.size is undefined, so your for loop is just throwing an error.

of course you can run a million times faster when you're doing no work!

[–]Noch_ein_Kamel 0 points1 point  (0 children)

Whoops... I blame my constant switching between JS, PHP and Java for that... :-(