Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lodash vs JavaScript Built In for map,reduce,filter

I was wondering whether it is better to include Lodash for these 3 functions [map(),reduce(),filter()] or just to use the ES6 versions of them.

I prefer using the Lodash functions, it's a little simpler for my use case. However, I am aware there is probably a performance benefit to using the ES6 functions.

Also was wondering whether Lodash is more backwards compatible that ES6?

Suggestions on how to test performance of my implementations?

Suggestions of whether to continue using Lodash or to use ES6?

like image 467
Jonathan Bartlett Avatar asked Jun 04 '17 11:06

Jonathan Bartlett


People also ask

What is the difference between filter map and reduce in JavaScript?

Both map and reduce have as input the array and a function you define. They are in some way complementary: map cannot return one single element for an array of multiple elements, while reduce will always return the accumulator you eventually changed.

Does lodash work with maps?

js. Lodash helps in working with arrays, collection, strings, objects, numbers etc. The _. map() method creates an array of values by running each element in collection through the iteratee.

What is the difference between filter and map in JavaScript?

Map: returns an array of pieces of information from the original array. In the callback function, return the data you wish to be part of the new array. Filter: returns a subset of the original array based on custom criteria.

Is lodash map faster?

The answer is that there is almost no difference. Lodash has its map which is pretty much a straight replacement of the browser one. Useful for backwards compatibility.


2 Answers

Lodash is a nice tool to use if you have more complex algorithms, its more readable etc.. It has built in functions for a lot of tasks which are not so easy to implement in native ES6, it is really handy and can save you from a lot of headache. But for simple tasks like you mentioned I would use ES6. As @Mayday said in a comment it is the future.

If you use Lodash only for these tasks I suggest you to get rid of it, that means you have one less dependency which is almost always a good thing (users don't have to download the lodash, because native map,reduce,filter are implemented in the browser). Yes nowadays you may need to use a bundler, or translator to make your code es5 compatible, but that is a dev-dependency which won't be there in production, and also it will be supported in a while and you won't even need these extra steps.

For testing your code see these answers: How do you performance test JavaScript code?

Also Google Chrome and Firefox have really good profilers: https://developers.google.com/web/tools/chrome-devtools/evaluate-performance/reference#record

If you want to compare native and lodash functions I suggest you to run the same functions implemented in both a few million times and measure the time between start and end (console.time https://developer.mozilla.org/en-US/docs/Web/API/Console/time), I think you should also make a few measurements of them, since the result can depend on a lot of other things. I think I would also avoid for loops, since they are highly optimized and could distort the results.

EDIT

As @TamasHegedus pointed out in a comment, these functions are in the ES5 specification so you don't even need a bundler, or a translator, it will work natively.

like image 145
godzsa Avatar answered Oct 15 '22 08:10

godzsa


Js perf can be used to test performance - as in this test from 2015 which shows lodash's Map being markedly faster than the native map function. I'm not sure to what extent this performance difference remains.

It's more common for web apps to feel slow due to loading large amounts of code, rather than that JavaScipt being too slow. If these are the only functions you plan to use from Lodash, I would suggest using Native es6 methods.

like image 30
John Whiles Avatar answered Oct 15 '22 08:10

John Whiles