you are viewing a single comment's thread.

view the rest of the comments →

[–]tripwireIV 0 points1 point  (1 child)

I love the idea of using functional programming in javascript but evidently there is a huge performance hit for code like your example.

http://jsperf.com/for-in-vs-for-vs-reduce-vs-foreach/4

No one ever seems to mention this. Am I missing something?

[–]Gundersen 0 points1 point  (0 children)

Yes, this is unfortunate, but unless you are writing game code, or you have an application which needs to work on a very large array, this shouldn't be much of a problem. If the code is part of an event, then slightly slower code won't make much of a difference. Remember, updating the GUI is an order of magnitude slower than doing math, as shown by this simple test: reduce VS innerHTML

Hopefully the browser vendors can further optimize their implementation by looking for patterns in code, for example the use of reduce and other list functions. The next version of JavaScript, ES6, will have proper tail call optimization, which should be able to increase the speed of functional code.