TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Use .forEach

1 pointsby karlcoelho1about 11 years ago

2 comments

declandewetabout 11 years ago
The reasons we don&#x27;t are pretty good ones. The first, and most important reason, is that native implementations of methods like `.forEach` are extremely slower than their native-for-loop equivalents. For casual side projects, this does not really matter, and some might argue that in newer browsers the efficiency loss is minimal. But when you&#x27;re developing a particularly JavaScript heavy client-side application, predominantly use .forEach, and your users will notice. The second reason is browser compatibility. `.forEach` is not supported in Internet Explorer 7 or 8. That is an extremely large chunk of potential users.<p>All that being said, there are definitely ways to gain the same functionality using a utility library like Underscore (or the much-faster-than-underscore Lodash). Instead of `array.forEach` it&#x27;s simply `_.forEach(array, function(item) { });` - <a href="http://lodash.com/docs#forEach" rel="nofollow">http:&#x2F;&#x2F;lodash.com&#x2F;docs#forEach</a>
workhere-ioabout 11 years ago
This one is even more concise and avoids the scoping issues that forEach have:<p>for (var i in cars) { console.log(cars[i]) }<p>Also, if you&#x27;re using JS on the frontend, forEach isn&#x27;t supported by all browsers: <a href="http://stackoverflow.com/questions/156696/which-web-browsers-natively-support-array-foreach" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;156696&#x2F;which-web-browsers...</a>