Concat function in Lodash will creates a new array that concat the array with any additional arrays and/or values.
It will not change the original array.
It's easy to use and simple.
Same goes to the Javascript native concat method. But what's the difference between this 2 method?
Well obviously, one is using libraries (Lodash), and another one is Javascript original concatenation.
The syntax also different. BUT, the native Javascript concat win. Let me show you.
I've run the test 5 times in a row, with about 1-2 seconds of interval and this is the result
Running Native concat first, then lodash concat
Test | Native Concat | Lodash Concat | Differences |
---|---|---|---|
1 | 0.318ms | 0.179ms ✅ | 0.139ms |
2 | 0.097ms ✅ | 0.16ms | -0.063ms |
3 | 0.094ms ✅ | 0.159ms | -0.065ms |
4 | 0.094ms ✅ | 0.16ms | -0.066ms |
5 | 0.09ms ✅ | 0.158ms | -0.068ms |
Running Lodash concat first and then native concat.
Test | Lodash Concat | Native Concat | Differences |
---|---|---|---|
1 | 0.525ms | 0.009ms ✅ | 0.516ms |
2 | 0.237ms | 0.008ms ✅ | 0.229ms |
3 | 0.247ms | 0.008ms ✅ | 0.239ms |
4 | 0.259ms | 0.008ms ✅ | 0.251ms |
5 | 0.25ms | 0.008ms ✅ | 0.242ms |
With obvious result, native javascript concat wins. This is only using small dataset. With big dataset, surely it will tak e more time to process. Although the result is sub-milliseconds, the timing is important to make our apps run faster.
I'm not saying do not use Lodash concat, but hey, if we have already concat method in built with javascript, it's better to use it. Unless you have other reason to use lodash concat function and your applications don't really care about response time.
Some of Lodash function do actually make our life easier. It is useful and it makes the development much easier and faster as we do not have to implement a complex logic to get the result. So, it is up to you!
We will try to cover up more of the differences between lodash and inbuilt function so that we can have a look later on!
Thank you for reading :D
Psstt pstt :p Do consider to love this article ❤️ and follow me! Why not right? It's FREE~
I would really appreciate it 👨🏻💻
Will be posting more on things related to AWS, Javascript, Python, Serverless and more!
Top comments (0)