Skip to content

Instantly share code, notes, and snippets.

@KoryNunn
Last active December 16, 2015 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KoryNunn/5488215 to your computer and use it in GitHub Desktop.
Save KoryNunn/5488215 to your computer and use it in GitHub Desktop.
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
Copy link

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