Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash uniq vs ES6 Set and sort with 2 arrays
(version: 1)
Benchmarking the action to join 2 string arrays, remove the duplicates values (we have 4 in this test) and then sorting them.
Comparing performance of:
Lodash uniq vs ES6 set and sort
Created:
one year ago
by:
Guest
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), ...new Set(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
4985402.5 Ops/sec
ES6 set and sort
605181.6 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark described tests two different approaches for merging and de-duplicating values from two string arrays, followed by sorting the results. Here's a breakdown of the two main techniques compared in this benchmark: ### 1. Lodash uniq - **Test Case**: `return _.uniq(...array1, ...array2);` - **Library**: Lodash is a popular JavaScript utility library that provides many useful functions for working with arrays, objects, and more. The `uniq` function is specifically designed to create a new array with unique values from the input arrays. **Pros**: - **Ease of Use**: Lodash provides a straightforward method to remove duplicates, making the implementation simple and readable. - **Performance**: Optimized for performance; Lodash has extensive testing and a well-documented codebase to ensure efficiency. **Cons**: - **Dependency**: Requires loading an external library (Lodash), which may increase the initial load time and add weight to the project if the library is not already being used elsewhere. - **Overhead**: For very simple operations, using a library can introduce unnecessary overhead compared to native solutions. ### 2. ES6 Set and Sort - **Test Case**: `return [...new Set(array1), ...new Set(array2)].sort();` - **JavaScript Feature**: This approach uses the ES6 `Set`, which is a built-in collection that can only hold unique values. When you create a new `Set` with an array, it automatically removes duplicates. The spread operator (`...`) is used here to convert the `Set` back into an array. **Pros**: - **No Dependencies**: It's a native JavaScript feature, meaning there’s no extra library to load or manage. This leads to a lighter footprint in applications. - **Modern Syntax**: Clean and concise syntax using ES6 features, making the code easier to read and maintain. **Cons**: - **Sorting Overhead**: Using `.sort()` directly after creating the array can incur additional computational costs, especially on large datasets, as sorting algorithms can have significant time complexity. - **Older Browsers Compatibility**: Some older environments may not support ES6 features fully, though this is less relevant in contemporary development. ### Performance Results From the benchmark results: - The **Lodash uniq** method executes at approximately **4,985,402.5 operations per second**. - The **ES6 set and sort** method executes at about **605,181.56 operations per second**. ### Alternatives Beyond the two tested methods, there are other alternatives that could be considered for similar functionality: - **Using a Plain Object**: You could build a function that iterates through both arrays and uses a plain object to keep track of seen values, aggregating unique items into a new array. This would be manual but lightweight. - **Filter with IndexOf**: Another approach could be using `Array.prototype.filter()` combined with `indexOf()` to check for duplicates, although this could become inefficient with large datasets due to its O(n^2) complexity. Selecting the appropriate method depends on the specific requirements of your project, including performance needs, readability, and whether or not you are already using external libraries. Each approach has its place, making it important to consider the context of your application.
Related benchmarks:
string array sort comparison
string array sort comparison 2
string array sort comparison 3
Array join and let unique values in return v2
Array.sort vs Math.min+Math.max with strings
Sorting array
Array find vs includes (temp3214)
Lodash Uniq vs ES6 set and sort
Lodash 4 uniq vs ES6 Set and sort
Comments
Confirm delete:
Do you really want to delete benchmark?