9

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?

  • 1
    ES6 is the future... I would totally go on that one – Mayday Jun 04 '17 at 11:13
  • @Mayday thank you for the speedy response – Jonathan Bartlett Jun 04 '17 at 11:14
  • 1
    Lodash is a nice tool to use if you have more complex algorithms, its more readable etc., They have build in functions for a lot of tasks, and it is really handy. It can save you from a lot of headache. But for simple tasks like you mentioned I would use ES6. As @Mayday said it is the future, and I guess it is the present too :D Also you lose one dependency if you get rid of Lodash if you can, which is almost always a good thing. – godzsa Jun 04 '17 at 11:25
  • @godzsa thank you very much, I'll say goodbye to Lodash until I need it for something much more complex then, if not for good – Jonathan Bartlett Jun 04 '17 at 11:26
  • *Suggestions on how to test performance of my implementations?* You can look into [JSPerf](https://jsperf.com/). – Rajesh Jun 04 '17 at 11:29
  • [These functions are es5.](http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.21) – Tamas Hegedus Jun 04 '17 at 12:20

2 Answers2

10

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.

godzsa
  • 2,105
  • 4
  • 34
  • 56
1

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.