Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash Uniq vs Javascript Set [1,000,000]
(version: 1)
Comparing performance of:
Lodash Uniq vs Javascript Set
Created:
9 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>
Script Preparation code:
var firstEqual = []; var secondEqual = []; for (var i=0; i<=1000000; i++) { firstEqual.push(i); secondEqual.push(i); } var arrayToDedup = [...firstEqual, ...secondEqual];
Tests:
Lodash Uniq
_.uniq(arrayToDedup);
Javascript Set
[...new Set(arrayToDedup)]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash Uniq
Javascript Set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 140 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash Uniq
1.6 Ops/sec
Javascript Set
1.8 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 9 months ago):
The benchmark defined in the provided JSON compares two methods of removing duplicates from an array: **Lodash's `uniq` function** and the native **JavaScript `Set` constructor**. Below, I explain the purpose of the test, the differences in approaches, pros and cons, and alternatives. ### Benchmark Overview 1. **Benchmark Options Compared** - **Lodash `uniq`**: The `uniq` method from the Lodash library is designed to create an array of unique values by removing duplicates from a given array. - **JavaScript `Set`**: The `Set` object is a built-in JavaScript feature that allows you to store unique values of any type, whether primitive values or object references. When you convert a Set back to an array, you get a deduplicated array. 2. **Benchmark Preparation**: - An array called `arrayToDedup` is created that contains integers from 0 to 1,000,000, each duplicated once. This array serves as input to both functions for the benchmark. ### Performance Results - **Lodash Uniq** executed at approximately **15.66 executions per second**. - **JavaScript Set** executed at approximately **14.59 executions per second**. ### Pros and Cons #### Lodash `uniq` - **Pros**: - Easy to use and understand as part of the Lodash library, which offers a variety of utility functions, making it a good choice if you are already using Lodash. - Potentially more powerful in terms of configuration and customization for more complex deduplication scenarios. - **Cons**: - Slower performance compared to native JavaScript, as shown in the benchmark. - Adds an additional dependency to your project, which may not be desirable if you are aiming for a lightweight solution. #### JavaScript Set - **Pros**: - Native to JavaScript, so it does not require external libraries, reducing dependencies and improving load times. - Likely to be faster in terms of performance, as native implementations are often optimized by JavaScript engines. - **Cons**: - Limited to unique primitive values; more complex deduplication based on object properties would require additional logic. - Developers who are unfamiliar with Sets might find it less intuitive than using Lodash. ### Other Considerations - **Code Size**: Using native JavaScript (`Set`) tends to keep the codebase smaller since no additional library is required. - **Readability and Maintainability**: Lodash is often praised for its functional utility style and easier syntax, which might make it more maintainable in some cases. - **Compatibility**: Ensure that the JavaScript environment you are targeting supports the `Set` constructor, as older environments might not. ### Alternatives - For cases requiring deduplication of an array of objects based on specific properties, a combination of the `Array.prototype.filter` method can be employed, leveraging a temporary object to track seen values. - Other libraries, such as **Underscore.js** (similar in purpose to Lodash), also offer similar deduplication functionalities. - Custom implementations using sorting or hashing algorithms, which may provide desired performance improvements under specific conditions. ### Conclusion Both Lodash's `uniq` and JavaScript's `Set` provide valuable methods for array deduplication, with trade-offs in performance, dependency management, and complexity. The choice between them should consider the specific needs of the project, performance requirements, and existing dependencies.
Related benchmarks:
Lodash Uniq vs Javascript Set
Lodash Uniq vs Javascript Set 21
Lodash Uniq vs Javascript Set2
Lodash Uniq vs Javascript Set 222
Lodash Uniq vs Javascript Set #2
Lodash Uniq vs Javascript Set and spread
Lodash Uniq vs Javascript Set 10000 items
Lodash Uniq vs Javascript Set
Lodash Uniq vs Javascript Set (100K)
Comments
Confirm delete:
Do you really want to delete benchmark?