Re:Stop writing For loops. Start using underscore.
by mathias on Monday February 10, 2014 @04:07AM
(#7209916)
Another example is:
_.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`.
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.