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.
我想知道将Lodash包含在这三个函数中是否更好[map(),reduce(),filter()]或者只是使用它们的ES6版本。
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.
我更喜欢使用Lodash函数,它对我的用例来说有点简单。但是,我知道使用ES6功能可能会带来性能优势。
Also was wondering whether Lodash is more backwards compatible that ES6?
还想知道Lodash是否更符合ES6的向后兼容性?
Suggestions on how to test performance of my implementations?
关于如何测试我的实现性能的建议?
Suggestions of whether to continue using Lodash or to use ES6?
是否继续使用Lodash或使用ES6的建议?
2 个解决方案
#1
8
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.
Lodash是一个很好的工具,如果你有更复杂的算法,它更具可读性等。它内置了许多任务的功能,这些功能在原生ES6中不是那么容易实现,它真的很方便,可以帮你节省很头疼。但对于你提到的简单任务,我会使用ES6。正如@Mayday在评论中所说,这是未来。
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.
如果你只为这些任务使用Lodash,我建议你去除它,这意味着你有一个较少的依赖,这几乎总是一件好事(用户不必下载lodash,因为原生地图,减少,过滤是在浏览器中实现)。是的,现在您可能需要使用捆绑器或转换器来使您的代码与es5兼容,但这是一个开发依赖项,它不会在生产中出现,并且它会在一段时间内得到支持,您甚至不会需要这些额外的步骤。
For testing your code see these answers: How do you performance test JavaScript code?
要测试代码,请参阅以下答案:如何测试JavaScript代码的性能?
Also Google Chrome and Firefox have really good profilers: https://developers.google.com/web/tools/chrome-devtools/evaluate-performance/reference#record
谷歌Chrome和Firefox也有很好的分析器: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.
如果你想比较native和lodash函数,我建议你运行几百万次实现的相同函数,并测量开始和结束之间的时间(console.time https://developer.mozilla.org/en-US/ docs / Web / API / Console / time),我认为你也应该对它们进行一些测量,因为结果可能取决于很多其他的东西。我想我也会避免for循环,因为它们经过高度优化,可能会扭曲结果。
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.
正如@TamasHegedus在评论中指出的那样,这些函数都在ES5规范中,所以你甚至不需要捆绑器或翻译器,它本身就可以工作。
#2
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.
Js perf可以用来测试性能 - 就像2015年的测试一样,它显示了lodash的Map明显比原生地图功能快。我不确定这种性能差异到底有多大。
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.
Web应用程序由于加载大量代码而感觉很慢,而不是JavaScipt太慢,这种情况更常见。如果这些是您计划在Lodash中使用的唯一功能,我建议使用Native es6方法。
#1
8
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.
Lodash是一个很好的工具,如果你有更复杂的算法,它更具可读性等。它内置了许多任务的功能,这些功能在原生ES6中不是那么容易实现,它真的很方便,可以帮你节省很头疼。但对于你提到的简单任务,我会使用ES6。正如@Mayday在评论中所说,这是未来。
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.
如果你只为这些任务使用Lodash,我建议你去除它,这意味着你有一个较少的依赖,这几乎总是一件好事(用户不必下载lodash,因为原生地图,减少,过滤是在浏览器中实现)。是的,现在您可能需要使用捆绑器或转换器来使您的代码与es5兼容,但这是一个开发依赖项,它不会在生产中出现,并且它会在一段时间内得到支持,您甚至不会需要这些额外的步骤。
For testing your code see these answers: How do you performance test JavaScript code?
要测试代码,请参阅以下答案:如何测试JavaScript代码的性能?
Also Google Chrome and Firefox have really good profilers: https://developers.google.com/web/tools/chrome-devtools/evaluate-performance/reference#record
谷歌Chrome和Firefox也有很好的分析器: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.
如果你想比较native和lodash函数,我建议你运行几百万次实现的相同函数,并测量开始和结束之间的时间(console.time https://developer.mozilla.org/en-US/ docs / Web / API / Console / time),我认为你也应该对它们进行一些测量,因为结果可能取决于很多其他的东西。我想我也会避免for循环,因为它们经过高度优化,可能会扭曲结果。
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.
正如@TamasHegedus在评论中指出的那样,这些函数都在ES5规范中,所以你甚至不需要捆绑器或翻译器,它本身就可以工作。
#2
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.
Js perf可以用来测试性能 - 就像2015年的测试一样,它显示了lodash的Map明显比原生地图功能快。我不确定这种性能差异到底有多大。
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.
Web应用程序由于加载大量代码而感觉很慢,而不是JavaScipt太慢,这种情况更常见。如果这些是您计划在Lodash中使用的唯一功能,我建议使用Native es6方法。