Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concern about native map and reduce speed #160

Open
stevensacks opened this issue May 26, 2016 · 1 comment
Open

Concern about native map and reduce speed #160

stevensacks opened this issue May 26, 2016 · 1 comment

Comments

@stevensacks
Copy link

stevensacks commented May 26, 2016

The native Javascript map and reduce array functions are ~134x slower than writing a loop, and ~2x as slow as Lodash (which is 67x times slower than writing a loop).

For example, doing a reduction where you double the value:

function reduce(arr, total = 0) {
    var i, len = arr.length;
    for (i = 0; i < len; i++)
    {
        total += arr[i] * 2;
    }
    return total;
}

An array with 100,000 integers (going from 0 to 99,999) gets reduced in 0.19ms using this function.

Native Array.reduce() takes 25.44ms.
arr.reduce((total, val) => total + (val * 2), 0)

Lodash takes 12.81ms
_(arr).reduce((total, val) => total + (val * 2), 0);

Lodash and native performance are abysmal by comparison. And while I know that arrays of 100,000 items are uncommon, it's worth noting.

I'm wondering if there's a way to use your reduce function to call my own insanely faster reduce instead of JavaScript's native reduce.

@aikeru
Copy link

aikeru commented Jan 3, 2017

Looks like this might do what you want...

(reduce, arr) => _.reduce(arr, reduce)

But native performance is expected to improve over time and may vary by browser and platform.

PS I'm not affiliated with 1-liners, though their work is very cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants