News for hackers. Stuff that matters.
Re:How I discovered my love for JavaScript after throwing 90... by satori99 on Sunday September 17, 2017 @07:53PM (#15272373)
> The for loop is now completely extinct in my codebase. If you do happen to stumble across one, point it out so I can kill it.

For loops in JS are significantly faster than .forEach() or .map()

When performance matters (WebAudio, WebGL etc), you can't easily get rid of for loops just yet.

Re:How I discovered my love for JavaScript after throwing 90... by fenomas on Sunday September 17, 2017 @10:00PM (#15272852)
This isn't a concern unless you're tuning performance and the loop is a bottleneck. forEach can be slower (if the JS engine doesn't inline the function body), but not to the extent that you need to structure your code around it.
Re:How I discovered my love for JavaScript after throwing 90... by satori99 on Sunday September 17, 2017 @10:05PM (#15272869)
Yeah, I agree with that. Which is why I mentioned performance sensitive stuff like WebGL.

Personally I use the functional methods whenever I can, but the original quote I re-posted indicates that the author thinks that for-loops should always be replaced with their functional equivalents, and I was just pointing out that this is not universally a good idea yet.

Re:How I discovered my love for JavaScript after throwing 90... by fenomas on Sunday September 17, 2017 @11:06PM (#15273108)
Yeah, I agree - just adding a caveat to your caveat.

Personally I found the whole article very skippable. None of it is necessarily terrible advice, but everything is overstated, and said without really explaining why its advice better than the alternatives.

Re:How I discovered my love for JavaScript after throwing 90... by dvdhnt on Sunday September 17, 2017 @08:53PM (#15272611)
Huh, do you have a source on that statement?

I'm not disagreeing with you, I actually don't know, and am always happy to learn useful facts for refactoring and improved performance :)

Re:How I discovered my love for JavaScript after throwing 90... by satori99 on Sunday September 17, 2017 @09:13PM (#15272698)
There are plenty of online benchmarks that show this. jsperf is down for me atm, so i can't provide a link.

Some of the popular JS frameworks actually re-implement an array.forEach function as an internal for-loop to mitigate this.

I am not sure of why this is the case, and I am also not sure if the situation has improved or not since I last had to deal with the performance difference.

edit: The mozilla page about this has a polyfill for the forEach method, that shows it does more than a simple for loop:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Re:How I discovered my love for JavaScript after throwing 90... by dvdhnt on Monday September 18, 2017 @11:42AM (#15276871)
Cool, thanks!
Re:How I discovered my love for JavaScript after throwing 90... by jazoom on Monday September 18, 2017 @07:31AM (#15274933)
I use for loops mainly when I want to be able to break;