r/javascript • • 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-69645e613049
0 Upvotes

18 comments sorted by

View all comments

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:

// _.js
const j = require('./j')
const _ = require('lodash')

_.map(j, item => {
    return item.num
})

...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):

// _.js
(_, j) => {
    return _.map(j, item => item.num);
}

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 map implementations are tested, this is the result I see:

Array.prototype.map 364ms
lodash.map 356ms

4

u/oculus42 May 13 '18

What version of NodeJS are you running? I modified and tested with three different Node versions and native in 10.1.0 was substantially faster, which is always nice to see.

2

u/stutterbug May 13 '18

I've changed to v10.1.0 (I was using current LTS before), and here are what my results are now (this is from a single run, but it is in line with the average):

Node version: v10.1.0
native: 0s 169.771374ms
lodash: 0s 158.513091ms

Here are the changes I made:

// vs.js (removing bits I didn't change)
const delay = (period) => new Promise(resolve => setTimeout(resolve, period));

// removing code I didn't change

await delay(2000)
await pMg.execPromise(`node meter ${fileA}`)
await delay(2000)
await pMg.execPromise(`node meter ${fileB}`)

// removing more code I didn't change


// for.js
const j = require('./j')

console.info(`Node version: ${ process.version }`);
const start = process.hrtime();

j.map( item => {
    return item.num
})

const [s, ms] = process.hrtime(start);
console.info(`native: ${ s }s ${ ms/1000000 }ms`);


// _.js
const j = require('./j')
const _ = require('lodash')

const start = process.hrtime();

_.map(j, item => {
    return item.num
})

const [s, ms] = process.hrtime(start);
console.info(`lodash: ${ s }s ${ ms/1000000 }ms`);

2

u/oculus42 May 13 '18

I eliminated the separate files altogether, then added Benchmark.js to give it better statistics.

2

u/stutterbug May 14 '18 edited May 14 '18

That is in line with what I saw on my own tests. Using the latest from your fork, here's the output with 10,000 items (running LTS):

native map x 5,958 ops/sec ±0.87% (86 runs sampled)
lodash x 6,393 ops/sec ±1.39% (85 runs sampled)
lodash/map x 7,038 ops/sec ±1.34% (85 runs sampled)
lodash.map x 7,010 ops/sec ±1.29% (85 runs sampled)
native for x 17,691 ops/sec ±0.80% (89 runs sampled)
Fastest is native for

If I increase the number of items to 1,000,000 on LTS, I get much poorer performance for native.

native map x 2.78 ops/sec ±1.45% (11 runs sampled)
lodash x 43.31 ops/sec ±2.83% (56 runs sampled)
lodash/map x 44.72 ops/sec ±2.83% (58 runs sampled)
lodash.map x 44.14 ops/sec ±3.05% (57 runs sampled)
native for x 25.58 ops/sec ±4.06% (46 runs sampled)
Fastest is lodash/map,lodash.map,lodash

On 10.1.0, things are better for native, but lodash still outperforms.

native map x 5.98 ops/sec ±1.14% (19 runs sampled)
lodash x 44.15 ops/sec ±5.77% (56 runs sampled)
lodash/map x 46.41 ops/sec ±5.12% (59 runs sampled)
lodash.map x 47.18 ops/sec ±5.58% (61 runs sampled)
native for x 28.65 ops/sec ±5.91% (50 runs sampled)
Fastest is lodash.map,lodash/map

But if you also test a form of your for test that doesn't use Array.prototype.push, you get the fastest results:

native map x 5.94 ops/sec ±3.13% (19 runs sampled)
lodash x 45.28 ops/sec ±4.23% (59 runs sampled)
lodash/map x 47.23 ops/sec ±5.86% (61 runs sampled)
lodash.map x 47.12 ops/sec ±4.88% (59 runs sampled)
native for x 28.26 ops/sec ±8.54% (49 runs sampled)
native for, non-push x 89.59 ops/sec ±1.90% (67 runs sampled)
Fastest is native for, non-push

The test looks like this:

'native for, non-push': function() {
    const result = new Array(data.length);
    let i;
    for (i = 0; i < data.length; i++) {
        result[i] = data[i].num;
    }
}

2

u/oculus42 May 14 '18

The fixed-length array is a good addition. I've called it "native for, fixed" and added it to the tests, as well as adding the Node version and test length to the output, to make keeping track easier.

I think 1,000,000 records is a more uncommon use case, but it is interesting to look at the data points.

Someone with more time and interest than I could execute a number of data sizes against the various versions of Node and plot a proper line graph.

2

u/stutterbug May 14 '18

Cool.

I agree that 1,000,000 items isn't a very plausible use case. I like using extreme datasets because they it draws out the subtle differences between the single start-up cost of a native function (usually just, but not always, allocating memory) and the incremental cost of each iteration. That isn't always actually useful in itself, but it can help you fine tune your code so that each iteration is as tight as possible.

I use these map, reduce, filter, etc., in hybrid mobile games a lot, so I deal with medium-sized arrays (800~10,000 items), restricted internal memory, slow virtual memory, underpowered cpus and so on, 60 times a second. Even then, I find there isn't really a good justification for perf-testing JS's built-in array methods (even though, obviously, I get a personal kick out of doing it). Where possible, I try to use typed arrays (which blows the doors off anything else), but where I can't I just try to be sensible with my iterative code and stick with whatever produces the most understandable module. When I see jank, then I start tuning, but like I said elsewhere, the problem almost always is in my iterative code, not in JS's internal algorithm.

3

u/Charuru May 13 '18

Facepalm. Embarrassed for OP

3

u/stutterbug May 13 '18

Honestly, I don't blame OP at all. Performance testing is hard to do and easy to mess up in totally subtle ways. Whenever I do perf testing, I never, ever believe what I see. If it confirms my expectations, I immediately reject it and look for where I screwed up. If its results are surprising, I reject it and look for where I screwed up. If its results are dull and unsurprising, I still look for where I screwed up. And I always keep the results to myself, because I am a coward.

One upvote for OP for trying and another upvote up for sharing.

1

u/allicanseenow May 13 '18

So you mean, with eager loading, lodash is faster than native map?

1

u/masklinn May 14 '18

More generally if you're not re-loading lodash before every call it's faster than native in Chrome.

This is a known problem

The native Array#map was always very slow in Chrome/V8 because AFAIK it's switching execution contexts between C++ and js. […] Previously [nb: Chrome 59 and older) it was 40-80 times slower, now [nb: Chrome 60 and younger] it's "only" 6-40 times slower.

In Firefox 58 the native Array#map is faster than both Chrome's native and FF's custom js