Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash 4 uniq vs ES6 Set and sort
(version: 1)
Benchmarking uniq vs using a set and sort it. 2 arrays to an array with removed duplicates and sorted
Comparing performance of:
lodash uniq vs ES6 Set and sort
Created:
one year ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
const array1 = [ "hijkl", "abc", "mn", "opqrs", "defg", "tuv", "wxy", "zab", "cde", "fghij", "klmno", "pqrst", "uvwxy", "zabcd", "efghi" ]; const array2 = [ "defg", "abc", "hijkl", "mn", "opqrs", "tuv", "wxy", "zab", "cde", "fghij", "klmno", "pqrst", "uvwxy", "zabcd", "efghi", "jklmn", "opqrs", "tuvwx", "yzabc", "defgh" ];
Tests:
lodash uniq
return _.uniq(...array1, ...array2);
ES6 Set and sort
return [...new Set([...array1, ...array2])].sort();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash uniq
ES6 Set and sort
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash uniq
3547904.2 Ops/sec
ES6 Set and sort
779326.9 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON is aimed at comparing two approaches for removing duplicates from two arrays and sorting the result: using **Lodash's `uniq` method** versus an **ES6 Set conversion followed by sorting the result**. ### Options Compared 1. **Lodash `uniq` Method**: - **Test Case**: `return _.uniq(...array1, ...array2);` - **Library Used**: Lodash is a utility library that provides modular methods for common programming tasks, including data manipulation and function programming. The `uniq` method specifically performs deduplication of an array. - **Pros**: - Lodash is optimized for performance and can handle various edge cases efficiently. - It simplifies code readability, as the method clearly indicates its intent to remove duplicates. - **Cons**: - Introduces a dependency on the Lodash library, which adds to the bundle size if used in a project. - May perform less efficiently under certain circumstances, depending on the internal implementation of `uniq`. 2. **ES6 Set and Sort**: - **Test Case**: `return [...new Set([...array1, ...array2])].sort();` - **Description**: This approach first creates a new Set from the merged arrays, which inherently removes duplicates. It then converts the Set back to an array and sorts it. - **Pros**: - Uses built-in JavaScript features, which means no additional library dependency is required. - Sets automatically enforce uniqueness, making them a straightforward choice for deduplication. - **Cons**: - The sorting step adds complexity and can slow down the process if there are many elements to sort. - The syntax might be less readable to those unfamiliar with ES6 features like spread operators and Sets. ### Performance Results From the benchmark results: - **Lodash `uniq`** executed **4,782,013 times per second**. - **ES6 Set and sort** executed **947,617.25 times per second**. This data demonstrates that the Lodash approach is significantly faster in this specific benchmarking scenario. ### Other Considerations When deciding between these methods, other considerations might include: - **Project Requirements**: If the project already uses Lodash or requires complex data manipulation functions, using Lodash could be beneficial. - **Browser Compatibility**: The ES6 approach may have compatibility issues in older browsers, whereas Lodash has broader compatibility. - **Code Readability and Maintainability**: The choice can depend on team familiarity with Lodash versus newer JavaScript syntax. Some teams might prefer the clear and explicit nature of Lodash. ### Alternatives Other alternatives to consider for deduplication and sorting tasks include: - **Array Methods**: Using `filter`, `indexOf`, and `sort`, such as: ```javascript return array1.concat(array2).filter((value, index, self) => self.indexOf(value) === index).sort(); ``` - **Map or an Object**: Using an object to track seen values may offer speed improvements in deduplication, especially for large datasets. - **Third-party libraries**: Other libraries like Ramda or Underscore.js provide similar functionalities that might be more suitable depending on project needs. Each of these alternatives may have varying performance characteristics and complexity, so the choice will depend on specific application requirements, the development environment, and team preferences.
Related benchmarks:
lodash/uniq vs filter+set vs array→set→array
has vs includes vs indexOf
has vs includes vs indexOf 10k records
Array.sort vs Math.min+Math.max with strings
Array.sort vs Math.min+Math.max with strings via sorter
Lodash difference vs filter and includes on large arrays
Array find vs includes (temp3214)
Lodash uniq vs ES6 Set and sort with 2 arrays
Lodash Uniq vs ES6 set and sort
Comments
Confirm delete:
Do you really want to delete benchmark?