Differences between lodash and underscore
Why would someone prefer either the lodash.js or underscore.js utility library over the other?
Lodash seems to be a drop-in replacement for underscore, the latter having been around longer.
I think both are brilliant, but I do not know enough about how they work to make an educated comparison, and I would like to know more about the differences.
I created Lo-Dash to provide more consistent cross-environment iteration support for arrays, strings, objects, and arguments
objects1. It has since become a superset of Underscore, providing more consistent API behavior, more features (like AMD support, deep clone, and deep merge), more thorough documentation and unit tests (tests which run in Node, Ringo, Rhino, Narwhal, PhantomJS, and browsers), better overall performance and optimizations for large arrays/object iteration, and more flexibility with custom builds and template pre-compilation utilities.
Because Lo-Dash is updated more frequently than Underscore, a lodash underscore
build is provided to ensure compatibility with the latest stable version of Underscore.
At one point I was even given push access to Underscore, in part because Lo-Dash is responsible for raising more than 30 issues; landing bug fixes, new features, & perf gains in Underscore v1.4.x+.
In addition there are at least 3 Backbone boilerplates that include Lo-Dash by default and Lo-Dash is now mentioned in Backbone's official documentation.
Check out Kit Cambridge's post, Say "Hello" to Lo-Dash, for a deeper breakdown on the differences between Lo-Dash and Underscore.
Footnotes:
arguments
objects. In newer browsers, Underscore methods ignore holes in arrays, "Objects" methods iterate arguments
objects, strings are treated as array-like, and methods correctly iterate functions (ignoring their "prototype" property) and objects (iterating shadowed properties like "toString" and "valueOf"), while in older browsers they will not. Also, Underscore methods like _.clone
preserve holes in arrays, while others like _.flatten
don't. Lo-Dash is inspired by underscore, but nowadays is superior solution. You can make your custom builds, have a higher performance, support AMD and have great extra features. Check this Lo-Dash vs Underscore benchmarks on jsperf and.. this awesome post about lo-dash:
One of the most useful feature when you work with collections, is the shorthand syntax:
var characters = [
{ 'name': 'barney', 'age': 36, 'blocked': false },
{ 'name': 'fred', 'age': 40, 'blocked': true }
];
// using "_.filter" callback shorthand
_.filter(characters, { 'age': 36 });
// using underscore
_.filter(characters, function(character) { return character.age === 36; } );
// → [{ 'name': 'barney', 'age': 36, 'blocked': false }]
(taken from lodash docs)
In addition to John's answer, and reading up on lodash (which I had hitherto regarded as a "me-too" to underscore), and seeing the performance tests, reading the source-code, and blog posts, the few points which make lodash much superior to underscore are these:
It's not about the speed, as it is about consistency of speed (?)
If you look into underscore's source-code, you'll see in the first few lines that underscore falls-back on the native implementations of many functions. Although in an ideal world, this would have been a better approach, if you look at some of the perf links given in these slides, it is not hard to draw the conclusion that the quality of those 'native implementations' vary a lot browser-to-browser. Firefox is damn fast in some of the functions, and in some Chrome dominates. (I imagine there would be some scenarios where IE would dominate too). I believe that it's better to prefer a code whose performance is more consistent across browsers.
Do read the blog post earlier, and instead of believing it for its sake, judge for yourself by running the benchmarks. I am stunned right now, seeing a lodash performing 100-150% faster than underscore in even simple, native functions such as Array.every
in Chrome!
The extras in lodash are also quite useful.
Here is a list of differences between lodash, and it's underscore-build is a drop-in replacement for your underscore projects.
链接地址: http://www.djcxy.com/p/4752.html上一篇: 如何将package.json中的每个依赖项更新为最新版本?
下一篇: lodash和下划线之间的区别