TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Follow publication

Javascript data structure with map, reduce, filter, and ES6

If you are a fan of javascript and use it daily, then you will love this 😃

Deepak Gupta
TDS Archive
Published in
5 min readMay 13, 2018

--

Javascript is a language that gives the freedom of writing code in any style, from imperative to declarative. Most programmers use imperative because either they are coming from OOPs background, maybe they love it, or they are not familiar with other styles. Before we dive into the declarative style which is functional programming (FP), let’s understand the differences between the two by looking at an example (if you already know the difference then you may skip few paragraphs)

Imperative

The imperative style is cool but imagine if there is a complex mathematics logic here then the size of code and the readability will suck. It increases the cognitive load when reading, and over time makes it easier to falter in reasoning and logic.

Also, the main complexity of this code snippet derives from the fact that instead of telling the computer what we want it to do, we are instructing it on how to do it.

Declarative

Now, this looks pretty clean, shorter, expressive, concise code, less error-prone, easier to maintain, and easier to debug. We are telling the computer what we want it to do rather how to do it.

The declarative approach is easily optimizable at the complier end and also has fewer side effects.

Note: if you are concerned about the performance of above two and other javascript function (map, reduce, filter, find) then you should read here for small data set and can view here for large data set(100–1000000)

--

--

TDS Archive
TDS Archive

Published in TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Deepak Gupta
Deepak Gupta

Written by Deepak Gupta

COO @Steer Protocol| Write about Blockchain & Javascript | Take it easy

Responses (7)

Write a response