So why reduce over map? In theory the answer is simple — reduce returns string in aggregated value when you pass empty string as second parameter .reduce(callback, initialValue) where map returns processed array rows so you need to join at the end to get a string.
There are myriads of solutions to Fizz Buzz online, some are super clever, some are hardly readable, some are fast and some are slow (looking at you “for loop” ;) but in my opinion the usage of reduce or map would be the most efficient in terms of workload vs. performance (the most performant would be the one that reduces amount of iterations to 28 as far as I know).
So why reduce over map?
In theory the answer is simple — reduce returns string in aggregated value when you pass empty string as second parameter .reduce(callback, initialValue) _where _map returns processed array rows so you need to join at the end to get a string.
But tests in browsers have yielded interesting results — chromium based browsers heavily favours reduce while map is being 60–70% slower:
(tried in chrome 81, 83, 85 — results are within error margin)
JSPerf Chrome 83
In Firefox we see much smaller difference between both, but still in favour of reduce, map is 25–30 % slower
Who else loves to write side-effects-free functions? I think we, as programmers, all do. Today, in this story, I will walk you through the basic principles of functional programming that will make your coding life easier.
I previously wrote about some common coding algorithms that you’ll run into on your job search. The article was received fairly well so I thought I’d do a couple more to help in your journey to becoming more familiar with algorithms.
This tricky Kadane’s Algorithm with a deceptively simple solution is a great introduction to more optimal problem-solving practices. Continue ...
This article will introduce the concepts and topics common to all programming languages, that beginners and experts must know!