Need advice about which tool to choose?Ask the StackShare community!
Underscore vs lodash: What are the differences?
Introduction
Underscore and lodash are both popular utility libraries for JavaScript. They provide a wide range of functions and methods to assist in manipulating arrays, objects, and other data types. While they have many similarities, there are a few key differences between Underscore and lodash that are worth mentioning.
Performance: One of the main differences between Underscore and lodash is their performance. Lodash is generally faster than Underscore when it comes to executing functions and operations. This is due to lodash's focus on optimized algorithms and performance improvements over Underscore.
Modularity: Lodash offers modularized functions, which means that you can import only the functions that you need, instead of importing the whole library. This can help reduce the size of your JavaScript bundle, as well as improve load times. On the other hand, Underscore does not provide this level of modularity, requiring you to import the entire library even if you only need a few functions.
Additional Functions: Lodash provides some additional functions that are not available in Underscore. These additional functions can be quite useful in certain scenarios, such as deep cloning objects, manipulating strings, and working with dates. Underscore, on the other hand, may not have these specific functions built-in.
Chaining: Lodash supports method chaining, which allows you to chain multiple functions together in a single expression. This can greatly simplify and enhance the readability of your code. Underscore, although it provides similar functions, does not offer built-in method chaining.
Compatibility: Lodash is designed to be more compatible with newer versions of JavaScript, including ECMAScript 6 (ES6) features. It implements many of the ES6 functions and methods that may not be available in older browsers. Underscore, on the other hand, may not have full support for these newer JavaScript features.
Community support: Lodash has a larger and more active community compared to Underscore. This means that there is a larger pool of developers who can provide support, contribute to the library, and create plugins or extensions for specific use cases. Additionally, lodash has better documentation, making it easier to learn and use for developers.
In summary, lodash offers better performance, modularity, additional functions, method chaining, compatibility with newer JavaScript versions, and has a larger and more active community compared to Underscore.
Fast Healthcare Interoperability Resources (FHIR) provides standard data objects in JSON format for the healthcare industry. Since JSON objects are hierarchical and tree-like, we had a need to defensively 'pluck' fields from our JSON objects and do lots of mapping. We tried jQuery and Underscore and a few other technologies like FHIRPath; but Lodash has been the most well supported, works in the most contexts, has the cleanest syntax, etc. We particularly like the ES6 version of Lodash, where we can import
the method names directly, without resorting to * or _ syntax. We got hooked on the 'get' function to defensively pluck fields from objects without crashing our user interface, and have found countless uses for the other lodash functions throughout our apps.
Lodash is great for developing and optimizing algorithms.
Pros of Lodash
- Better than Underscore2
- Simple1
- Better that Underscore0
Pros of Underscore
- Utility85
- Simple55
- Functional programming40
- Fast32
- Open source28
- Backbone20
- Javascript16
- Annotated source code8
- Library6
Sign up to add or upvote prosMake informed product decisions
Cons of Lodash
- It reduce the performance1