Stop writing For loops. Start using underscore.
joelhooks.com
joelhooks.com
And I know that these are just examples, but what is the point of writing:
_.each(someArray, function(someThing) {
doSomeWorkOn(someThing);
})
when you can simply write: _.each(someArray, doSomeWorkOn);Faster and more complete.
A lib can offer leeway and enhancements over bare metal language APIs. For example, in Lo-Dash you can do callback shorthands like `_.every(buttons,'disabled')` instead of `buttons.every(function(button){return button.disabled});`.
Lo-Dash's methods are more consistent than shims and in many cases faster than their ES5 counterparts. Shims have to follow the language specification which means they have a lot more overhead than their Lo-Dash equivalents making them slower in older environments where performance is already an issue. Shims also fail to address engine issues like how IE<9 treats arrays like `[1, undefined, 3]` as sparse when no other engine does. Lo-Dash smooths over these issues by treating all arrays as dense and gets better performance in the process.
Utility libs, like Lo-Dash, can give you more consistency, generally better performance, and more features than their ES5 counterparts.
_.forEach(array, function(item) {
doSomething();
if (condition) {
// exits iteration early
return false;
}
});
This is just not possible with native `Array#forEach` – you’d have to use a plain `for` loop with a conditional `break` statement instead, or rewrite your code to use `Array#some`.(I know there's IE, but ... what about just fixing Array.prototype.forEach for those? "if (!Array.prototype.forEach) Array.prototype.forEach = ...")
In reality, it hasn't been an issue. Trivial to profile and optimize where required/needed vs standardizing on verbose, up front, YAGNI performance gains.
Lodash benchmarks must faster than underscore in a lot of areas, and addresses at least some of the perf issues while keeping the tasty sugar.