_.each()
is not fully compatible to [].forEach()
. See the following example:
var a = ['a0'];
a[3] = 'a3';
_.each(a, console.log); // runs 4 times
a.forEach(console.log); // runs twice -- that's just how [].forEach() is specified
So lodash's implementation is missing an if (... in ...)
check, which might explain the performance difference.
As noted in the comments above, the difference to native for
is mainly caused by the additional function lookup in your test. Use this version to get more accurate results:
for (var ix = 0, len = a.length; ix < len; ix++) {
cb(a[ix]);
}