You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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:
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.The text was updated successfully, but these errors were encountered: