r/programming • 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-69645e6130491
u/bananajsjs May 14 '18
Thanks to everyone correcting the experiment mistake, Requiring Lodash module should have been considered. And not result was not very surprising , they performance more or less the same.
4
u/RufusROFLpunch May 13 '18
Is es6 map actually implemented in Javascript? It wouldn't surprise me if such a common and useful function were implemented in the VM itself for optimization.
2
u/masklinn May 14 '18 edited May 14 '18
Is es6 map actually implemented in Javascript?
No (edit: in Chrome, "ES6" is a spec it does not require specific implementations as far as I know), but that's not necessarily a good thing: it requires jumping contexts between JS, C++ and back to JS (for the callback) and means the JIT can't inline and unroll (when applicable): the builtin is opaque. On the other hand having to handle sparse arrays per-spec is absolute hell on JS implementations.
Chrome/v8 went from JS to native about a year ago I think they'd gone native->JS a few years previously but can't find it.
I'm actually surprised the native version is faster than lodash, I guess their weird implementation slows it down enough of triggers an edge case because the normal behaviour is that native loses by at least 6x (in Chrome, not in Firefox)
edit: I have not checked, but according to a /r/nodejs comment the benchmark here apparently includes the loading time for lodash…
/r/javascript confirms, the benchmark includes require-ing the library…
1
2
u/scaleable May 13 '18
Try requiring lodash/map instead of the whole package to see it it changes something...
1
u/MenziesTheHeretic May 13 '18
Measure the time of the computation only. Perhaps the require statements influence the results.
8
u/Y_Less May 13 '18
This was just a terrible article:
Way too short, didn't actually explore or explain the results at all'll. Other problems:
What was the exact data used for these results?
What machine?
What browser?
Why are the result rows not explained?
How many repeats were done?
Any more information on rigour behind the tests?
How different are the results with a complex lambda compared to a trivial one, i.e. what percentage of a long run would map overhead really produce?
No mention of the fact that lodash isn't trying to be the fastest - one of its stated aims is to provide consistent performance across browsers, even if that's slightly slower on some.