Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A simplified forEach loop that's faster than Array.forEach
function fastEach(items, callback) {
for (var i = 0; i < items.length && !callback(items[i], i, items);i++) {}
return items;
}
module.exports = fastEach;
@DamonOehlman

This comment has been minimized.

Show comment Hide comment
@DamonOehlman

DamonOehlman May 3, 2013

Any value in caching the items length? This is sometimes recommend in performance optimisations. Difference will be minimal, but still, you don't want somebody making a fastereach module ;)

for (var i = 0, count = items.length; i < count && !callback(items[i], i, items);i++) {}

Any value in caching the items length? This is sometimes recommend in performance optimisations. Difference will be minimal, but still, you don't want somebody making a fastereach module ;)

for (var i = 0, count = items.length; i < count && !callback(items[i], i, items);i++) {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment