4 ms·
I'd like to point out that if jQuery fell back to the fundamental Array.prototype.forEach function when available, $.each would be /much/ faster than a for loop
by warfangle 15y ago
I'd like to point out that if jQuery fell back to the fundamental Array.prototype.forEach function when available, $.each would be /much/ faster than a for loop. Array.prototype.forEach, when available, is native code.
- timdown 15y agoNo. forEach() being native doesn't prevent it from generally being slower than a foor loop, because it still has to do a function call on every iteration.
- warfangle 15y agoIt depends on what you're doing within the loop. With a for loop, you still have to perform an array lookup. For something simple, like summing up all the numbers in an array, forEach is definitely faster. Try JSLitmus'ing it.
- swanson 15y agoThis is how underscore.js implements each()