FizzBuzz using . reduce in JavaScript

FizzBuzz using . reduce in JavaScript

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)

Image for post

JSPerf Chrome 83

In Firefox we see much smaller difference between both, but still in favour of reduce, map is 25–30 % slower

Image for post

algorithms programming code javascript fizzbuzz

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Solve ‘FizzBuzz’ in JavaScript

A simple, readable JavaScript solution for the ‘FizzBuzz’ coding challenge. I will post my own code snippets/solution towards the end, but for now, I want you to try and tackle these steps on your own as I explain.

Principles of Functional Programming in JavaScript That Will Make Your Coding Life Easier

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.

Coding Interviews: Common Algorithms

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.

Kadane’s Algorithm: Gateway to Dynamic Programming

This tricky Kadane’s Algorithm with a deceptively simple solution is a great introduction to more optimal problem-solving practices. Continue ...

Coding 101: Programming Language Building Blocks

This article will introduce the concepts and topics common to all programming languages, that beginners and experts must know!