最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ecmascript 6 - Lodash vs JavaScript Built In for map,reduce,filter - Stack Overflow

programmeradmin4浏览0评论

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 patible that ES6?

Suggestions on how to test performance of my implementations?

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

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 patible that ES6?

Suggestions on how to test performance of my implementations?

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

Share Improve this question asked Jun 4, 2017 at 11:11 Jonathan BartlettJonathan Bartlett 5121 gold badge4 silver badges12 bronze badges 6
  • 1 ES6 is the future... I would totally go on that one – Mayday Commented Jun 4, 2017 at 11:13
  • @Mayday thank you for the speedy response – Jonathan Bartlett Commented Jun 4, 2017 at 11:14
  • 1 Lodash is a nice tool to use if you have more plex 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 Commented Jun 4, 2017 at 11:25
  • @godzsa thank you very much, I'll say goodbye to Lodash until I need it for something much more plex then, if not for good – Jonathan Bartlett Commented Jun 4, 2017 at 11:26
  • Suggestions on how to test performance of my implementations? You can look into JSPerf. – Rajesh Commented Jun 4, 2017 at 11:29
 |  Show 1 more ment

2 Answers 2

Reset to default 12

Lodash is a nice tool to use if you have more plex 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 ment 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 patible, 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./web/tools/chrome-devtools/evaluate-performance/reference#record

If you want to pare 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/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 ment, these functions are in the ES5 specification so you don't even need a bundler, or a translator, it will work natively.

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 mon 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.

发布评论

评论列表(0)

  1. 暂无评论