Wednesday 6 June 2018

underscore.js - 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.


Answer



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:




  1. Underscore has inconsistent support for arrays, strings, objects, and 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.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...