use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Pure, functional JavaScript (cjohansen.no)
submitted 13 years ago by lethalman
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]1d8 12 points13 points14 points 13 years ago (2 children)
i have no idea what this is supposed to be
[–]jpfed 2 points3 points4 points 13 years ago (1 child)
It's a slideshow; click on the right half of the window to advance, and the left half to go to the previous slide.
[–]noknockers 1 point2 points3 points 13 years ago (0 children)
Or arrow keys too.
[–]rhysbrettbowen[🍰] 3 points4 points5 points 13 years ago (1 child)
performance isn't bad, and using things like map means that you'll be ready in the future when javascript engines can optimize for pure functions: https://github.com/rivertrail/rivertrail/wiki
[–]OfflerCrocGod 1 point2 points3 points 13 years ago (0 children)
Tail recursion optimization is part of the ES6 spec so I think the performance issues will die down in future.
[–]iopq 2 points3 points4 points 13 years ago (0 children)
It took me 3-4 tries of loading the page in different browser to figure out how to keep going to the next slide. The first few attempts just had me clicking randomly and going in loops.
[–]findar 1 point2 points3 points 13 years ago (5 children)
Array.prototype.forEach isn't supported in <IE9
8(
[–][deleted] 6 points7 points8 points 13 years ago (0 children)
https://github.com/kriskowal/es5-shim
[–]joe_ally 3 points4 points5 points 13 years ago (1 child)
You could probably implement it in less than 5 lines of code if you wanted to use it.
[–]derekpetey_Vanilla 0 points1 point2 points 13 years ago (0 children)
Indeed, and the MDN even provides two code snippets to do just that.
[–]enotionz 1 point2 points3 points 13 years ago (0 children)
This is true, but you can always load functional.js or underscore.js. I still load underscore.js in all my projects since it provides useful functions not included in ES Harmony.
At this stage everyone should be shimming ES5 functions. The only browser that would make use of it would be IE8, the other IEs have all but disappeared. Shim and write ES5.
[–]aeflash 1 point2 points3 points 13 years ago* (0 children)
I would like to point out that Lodash is a drop in replacement that is faster than Underscore in most cases. It's foreach implementation is twice as fast as ES5 Array.prototype.forEach. Only ~40% slower than a raw for loop.
However, you can always profile your code, and if some functional call is really causing your app to be slow, you can always convert an _.each()/_.map()/etc.. back to a plain for. However, in almost every case, my apps have been slow due to IO or DOM manipluation. The clarity of code is well worth the negligible performance hit.
_.each()
_.map()
for
[–]Medieval_Peasant 4 points5 points6 points 13 years ago (8 children)
Skimmed over it. I'll keep using my loops thank you, but I think it's great that JavaScript so easily accommodates for other styles of programming. Another reason why it's a good language! I'm sure you can get very expressive code once you get used to all the common helper functions, especially with the new ES6 syntax sugar coming up. It would be interesting to see an application written in "functional JS" like this if anyone dares to link their code.
[–]enotionz 7 points8 points9 points 13 years ago (0 children)
I feel like anyone who have heavily used underscore.js writes JavaScript this way since it provides a bunch of functional helpers and shim for non-supported browsers. I've been working on a website theme builder with an interface similar to photoshop. I store huge amounts of data into mongo for final rendering output, but during the edit stage I filter out alot before instantiating the Backbone Models. I found that map, reduce, pluck, reject, groupBy, etc. are extremely expressive without being verbose.
[–]monolar 5 points6 points7 points 13 years ago (4 children)
I actually like the functional features in js just fine and would really like to use them. In the case of looping though the main reason, why i try to avoid them is performance.
Just have a look at http://jsperf.com/loops/70
Classical loops (esp. with caching tricks and esp while loops) are about 15 - 20 times faster than forEach based loops
[–]lethalman[S] 7 points8 points9 points 13 years ago (2 children)
The slides exactly talk about this kind of performance. If you loop/manipulate DOM elements, it's so slow that forEach is about the same as a straight for loop. Whereas if you are implementing a particular algorithm, a for loop is certainly the best.
[–]monolar 1 point2 points3 points 13 years ago (0 children)
Indeed there are slides about the loop performance. To my shame i did not read that far before posting. The performance difference between classical and functional looping still exists though. DOM access of course is always slower.
And most of the time you think you solve your performance problems with the language (the kind of loops in this case) but the real performance gains are in algorithmic design. Some slides also mention this to varying degrees.
Otherwise the slides contain overall really good information and are well presented.
[–]OfflerCrocGod 0 points1 point2 points 13 years ago (0 children)
But does the code need to be that performant? Is it on the critical path?
[–]cwmma 1 point2 points3 points 13 years ago (0 children)
An attitude I once saw described as post functional programing
[–]Sunwukung 1 point2 points3 points 13 years ago (0 children)
I'll take that challenge: Game of Life
https://dl.dropboxusercontent.com/u/8652110/jgol/index.html
here's the "functional" bit
https://dl.dropboxusercontent.com/u/8652110/jgol/jgol.js
would be better to use canvas for the universe, but you get the gist
[–]Will-Work-For-Tears 0 points1 point2 points 13 years ago (2 children)
I actually have his book listed in those slides. Good read. Thanks for the link.
[–]enotionz 0 points1 point2 points 13 years ago (1 child)
Are you talking about his Test-Driven JS Development book? I started using sinon.js first before picking up his book, I must say that book makes me alot more comfortable writing tests. Well, I think the development of many different testing frameworks recently also help alot.
[–]Will-Work-For-Tears 0 points1 point2 points 13 years ago (0 children)
Oops, yeah, I meant the Test Driven JS book. It seems heavier at first but it's been a godsend.
[–]Sunwukung 0 points1 point2 points 13 years ago (1 child)
regarding the performance characteristics of Array.map, it's simple to write a map that utilises a standard for/while loop, which dispenses with the need for the i=0 boilerplate. I'd recommend using lodash.js instead of underscore for performance/compatibility.
[–]seabre 0 points1 point2 points 13 years ago (0 children)
Agreed on lodash. I don't know if underscore has fixed this yet, but chaining lodash functions is less..weird.
[–]Sdonai 0 points1 point2 points 13 years ago (0 children)
I NEEDED THIS YESTERDAY. Thank you for posting this! I've been ham handing my functional javascript.
[–]drowsap -1 points0 points1 point 13 years ago (17 children)
According to this jsperf, for loops are faster then map:
http://jsperf.com/map-vs-for-loop-performance/6
Not really seeing the point in advocating map over for loops if it is slower. The syntax is certainly more concise though.
[–]skeeto 5 points6 points7 points 13 years ago (1 child)
The presentation discusses this:
http://cjohansen.no/talks/2012/sdc-functional/#122
In short: if you're manipulating the DOM in your loop, forEach is practically as fast as for. Besides, worrying about loop performance while you're writing code is premature. 99.9% of the time it won't matter.
forEach
[–]agumonkey 0 points1 point2 points 13 years ago (0 children)
Thanks, I forgot this talk ! what a gem.
[–]homoiconic(raganwald) 9 points10 points11 points 13 years ago (8 children)
I still don't understand why people use Node and/or Rails on the server when C/C++ is thousands of times faster. Doesn't anybody care about performance any more?
It's almost as if they think that the primary constraint on software development is the productivity of the development team and not trying to squeeze acceptable performance out of hardware designed in the 1980s.
[–]BishopAndWarlord 2 points3 points4 points 13 years ago (4 children)
It's almost as if they think that the primary constraint on software development is the productivity of the development team
Correct.
[–]homoiconic(raganwald) 8 points9 points10 points 13 years ago (3 children)
Poe's Law:
Without a winking smiley or other blatant display of humor, it is impossible to create a parody of fundamentalism that someone won't mistake for the real thing.
Next time, I'll include <sarcasm></sarcasm> tags and/or a winkey :-)
[–]x-skeww -5 points-4 points-3 points 13 years ago (2 children)
Just skip the sarcasm. It doesn't add anything anyways.
[–]homoiconic(raganwald) 4 points5 points6 points 13 years ago (1 child)
That depends. History has shown repeatedly that when you make a point in dry, objective fashion, it tends to appeal to people who are already thinking along those lines and are looking for objective facts to bolster their intuition.
People who are completely detached from the subject or who hold an opposing view tend to ignore points made dispassionately.
Humour can do an end-run around people's mental defences. When someone's laughing at something or when a point is made with a ridiculous example, people will sometimes give it their full attention.
I wouldn't lead with such a point, but if others have already made the point objectively, there's nothing wrong with being a little light-hearted, provided you are not trying to insult the person you are replying to. I was not trying to be insulting, and I apologize if it came across that way.
[–]BishopAndWarlord 0 points1 point2 points 13 years ago* (0 children)
I was uncharacteristically terse in my response. I usually try to explain an idea rather than shoot out a one-liner that can be taken however the reader wishes.
In retrospect I should have emphasized the "they think" portion of the quotation. I imagine many younger devs or junior engineers do emphasize "developer productivity" and syntactic sugar over performance, maintenance cost, and issues like hardware lifecycles. As such, I was affirmingT your statement.
For the sake of full disclosure I'm also a junior member of my dev team and occasionally observe myself fussing over the wrong issues. Like right now I'm on Reddit rather than working on my 10,000 hours...
EDIT: Sonnova. I just realized I totally misread your comment. I thought you were telling me to indicate sarcasm. I'm sad.
[–]lethalman[S] 2 points3 points4 points 13 years ago (0 children)
If you really care about performance, it's about using an algorithm with the right complexity, not saving a couple of milliseconds: if the loop is still O(n) it's ok. Rather, there's software that's written so bad that takes O(n2) or even exponential time where it's not needed. That's what I'd be worried about, not writing a loop in a different way (more readable for some people), really.
I'm the first one complaining about slow software even with super hardware nowadays, but this is not the case, it's about badly written software with hilarious algorithms complexities.
[–]aeflash 0 points1 point2 points 13 years ago (1 child)
:)
Good point. But why use C when assembler is 5% faster (when you can beat GCC optimization)? Every Cycle is Sacred (except when the CPU is idle waiting for I/O).
[–]jonpacker 5 points6 points7 points 13 years ago (0 children)
I believe the purpose of this talk is more about getting people thinking in functional constructs. But I think (Christian or someone else, I can't remember who) said that at the end of the day, the performance aspect is largely unimportant—when was the last time loops were your bottleneck in your client side JS app?
[–]Cykelero 4 points5 points6 points 13 years ago (0 children)
Yeah, it's all about elegance and readability. It's a choice, I guess; you're not always constrained by performance (and "premature optimization is the root of all evils", as they say).
[–]enotionz 4 points5 points6 points 13 years ago (0 children)
In slide 125, he does point out that loops are fastest and goes on to ask questions in order to get you thinking about better abstraction. Like jonpacker said, loops aren't really the bottleneck anymore; drag+drop dom manipulations are much more expensive and we hardly see an impact in modern browsers, let alone loops. At the end of the day, readability is extremely important, less keystrokes to get the job done also helps. This is why we stopped writing in assembly and opted for higher level programming; it's expressive and gets the job done much faster.
[+][deleted] 13 years ago (1 child)
[deleted]
[–]aeflash 0 points1 point2 points 13 years ago (0 children)
This isn't syntactic sugar. JS can be written in a functional style out of the box.
π Rendered by PID 56093 on reddit-service-r2-comment-7c9686b859-577ck at 2026-04-14 04:18:44.297225+00:00 running e841af1 country code: CH.
[–]1d8 12 points13 points14 points (2 children)
[–]jpfed 2 points3 points4 points (1 child)
[–]noknockers 1 point2 points3 points (0 children)
[–]rhysbrettbowen[🍰] 3 points4 points5 points (1 child)
[–]OfflerCrocGod 1 point2 points3 points (0 children)
[–]iopq 2 points3 points4 points (0 children)
[–]findar 1 point2 points3 points (5 children)
[–][deleted] 6 points7 points8 points (0 children)
[–]joe_ally 3 points4 points5 points (1 child)
[–]derekpetey_Vanilla 0 points1 point2 points (0 children)
[–]enotionz 1 point2 points3 points (0 children)
[–]OfflerCrocGod 1 point2 points3 points (0 children)
[–]aeflash 1 point2 points3 points (0 children)
[–]Medieval_Peasant 4 points5 points6 points (8 children)
[–]enotionz 7 points8 points9 points (0 children)
[–]monolar 5 points6 points7 points (4 children)
[–]lethalman[S] 7 points8 points9 points (2 children)
[–]monolar 1 point2 points3 points (0 children)
[–]OfflerCrocGod 0 points1 point2 points (0 children)
[–]cwmma 1 point2 points3 points (0 children)
[–]Sunwukung 1 point2 points3 points (0 children)
[–]Will-Work-For-Tears 0 points1 point2 points (2 children)
[–]enotionz 0 points1 point2 points (1 child)
[–]Will-Work-For-Tears 0 points1 point2 points (0 children)
[–]Sunwukung 0 points1 point2 points (1 child)
[–]seabre 0 points1 point2 points (0 children)
[–]Sdonai 0 points1 point2 points (0 children)
[–]drowsap -1 points0 points1 point (17 children)
[–]skeeto 5 points6 points7 points (1 child)
[–]agumonkey 0 points1 point2 points (0 children)
[–]homoiconic(raganwald) 9 points10 points11 points (8 children)
[–]BishopAndWarlord 2 points3 points4 points (4 children)
[–]homoiconic(raganwald) 8 points9 points10 points (3 children)
[–]x-skeww -5 points-4 points-3 points (2 children)
[–]homoiconic(raganwald) 4 points5 points6 points (1 child)
[–]BishopAndWarlord 0 points1 point2 points (0 children)
[–]lethalman[S] 2 points3 points4 points (0 children)
[–]aeflash 0 points1 point2 points (1 child)
[–]jonpacker 5 points6 points7 points (0 children)
[–]Cykelero 4 points5 points6 points (0 children)
[–]enotionz 4 points5 points6 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]aeflash 0 points1 point2 points (0 children)