I was reading Death of for-loop article, and I thought maybe I should timeit the codes. I created this benchmark at jsperf which clearly shows that map-filter approach is about 90% slower.So, why is FP code here is slower? and also, why be functional if it is slower?The author was only talking about readability when it comes to FP and not efficiency or other execution factors when in real world, they are the preferred parameters.Rephrasing, What are the real world factors to use functional programming apart from readability?Pardon me for being lazy before. I am not really a writer and have trouble expressing my thoughts in written form.Added: I found this MPJ Video which I also found to be helpful.
Why Functional Programming - When is it okay to get rid of efficiency for readability?
javascript;functional programming
As a few people already explained in the comments, the way you did your benchmarks make them irrelevant. If, however, there was an actual benchmark showing that the functional variant was indeed slower, then keep reading.The author was only talking about readability when it comes to FP and not efficiency or other execution factors when in real world, they are the preferred parameters.That's not quite right.In real world, for most applications, a few microseconds you can optimize for a task performed by a user ten times per day doesn't mean much. Actually, it doesn't mean anything. In other words, such optimization is completely irrelevant for your product.However, the time you spend:Optimizing,Reading code which is unreadable,Fixing unreadable code which had bugs in it, because of its unreadability,Fixing the bugs you introduced in an unreadable codeleads to hours, days, weeks and months of additional work, which translates directly to thousands of dollars wasted making and maintaining the product in the first place.Therefore, readability is the preferred parameter in the real world, because there is real money involved there.