r/javascript • u/bananajsjs • May 13 '18
[Performance] Lodash vs ES6 : map()
https://medium.com/@peterchang_82818/es6-lodash-native-javascript-nodejs-example-tutorial-map-function-functional-programming-performance-speed-69645e6130498
3
May 13 '18
Would be interesting to show the results over multiple Node versions (the article makes no mention of the version used). I suppose a newer V8 version has introduced some optimizations compared to the 2015 situation, which might explain why the tables have turned.
For what it’s worth, I don’t think it’s too crazy that lodash was faster, because it doesn’t need to handle shallow arrays and their implementation doesn’t need to cross relatively slow JS/C++ boundaries unlike a naive native map() implementation. Of course, a clever JIT could just special-case the map() call and get rid of the lambda overhead altogether, which is what I hope V8 did.
3
u/voidvector May 13 '18
If you are going to micro-benchmark, you might as well add for.
for would out-perform both in 90% of the real world scenarios due to all the compiler optimization available (e.g. loop unrolling, loop invariant code motion, memory fragmentation, garbage collection).
3
u/oculus42 May 13 '18
I added a for loop to my fork's benchmark test:
In Node 10.1.0, map is faster (in this run).
native map x 22,775 ops/sec ±19.38% (85 runs sampled) lodash x 7,761 ops/sec ±0.86% (88 runs sampled) lodash/map x 8,874 ops/sec ±1.32% (92 runs sampled) lodash.map x 9,313 ops/sec ±0.97% (90 runs sampled) native for x 18,955 ops/sec ±1.23% (94 runs sampled) Fastest is native mapNote that the margin of error is also higher on the map. Subsequent runs were trading or sharing the "fastest" title between the two native functions.
3
May 13 '18
[removed] — view removed comment
2
u/oculus42 May 13 '18
I forked and modified the tests to eliminate the script loading, and did a version with benchmark after reading this.
I tested three Node versions, and was surprised to see the performance difference in Node 10 is substantial.
Good to see native getting faster.
48
u/stutterbug May 13 '18 edited May 13 '18
When I ran these tests and saw it took 30ms on my machine to map over only 10,000 items in lodash, my bullshit meter broke. Mapping over 10,000 items in any modern library should never take more than a a couple milliseconds at the most (except DOM elements, which are a different problem). If you have more than a million items, I'd believe it. Running it on a mobile device? I'd believe it. You have some crazy complex callback, I'd believe it. This? No way.
So, in this case, the total time for lodash includes the time spent loading lodash. When you account for that and just test the map, you get much more reasonable execution times and more plausible perf differences. I'd be glad to do a pull-request, but I don't know how to fix these tests because I have never worked with meter. But instead of doing this:
...load the dependencies ahead of time and inject them into your tests (and allow the engine a couple seconds to settle after starting any tests, and a second or two between test runs to allow for GC as data falls out of scope):
Also, for the beginners reading these things and using it to decide on native vs lodash... stop right now. The performance difference you see here is almost always irrelevant in most applications. Unless you are dealing with truly massive amounts of data or have crazy amounts of recursion, this kind of test is just a fascinating diversion. You may be able to eke out a millisecond here or there, but performance bottlenecks are NEARLY ALWAYS found in your own code.
[edit] For the record, if I bump up the number if items in the array to 1,000,000, inject some delays before each test, and just test the execution time within the two test so that only the
mapimplementations are tested, this is the result I see: