Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniq vs array to set to array 1
(version: 1)
Comparing performance of:
Set vs Lodash
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
Tests:
Set
var l = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; return [... new Set(l)];
Lodash
var l = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; return _.uniq(l);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set
Lodash
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/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
6178779.0 Ops/sec
Lodash
18011210.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
This benchmark evaluates two different methods for creating a unique array from an array containing duplicate values in JavaScript. Specifically, it compares the performance of using **Lodash's `_.uniq` method** against a native JavaScript approach using **the Set constructor combined with the spread operator**. ### Test Cases Overview 1. **Set Approach** - **Benchmark Definition**: `var l = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; return [... new Set(l)];` - **Test Name**: Set - **Description**: This approach creates a Set from the array `l`, which inherently allows only unique values. The spread operator (`...`) is then used to convert the Set back into an array. 2. **Lodash Approach** - **Benchmark Definition**: `var l = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; return _.uniq(l);` - **Test Name**: Lodash - **Description**: This uses Lodash's `uniq` function, which is designed to iterate over the array and filter out duplicates, returning a new array with unique values. ### Performance Results The results of the benchmark indicate how many operations were executed per second for each method: - **Lodash**: 18,011,210 executions per second - **Set**: 6,178,779 executions per second ### Pros and Cons #### Set Approach (Using Set + Spread Operator) - **Pros**: - **Native**: Relies solely on built-in JavaScript features, making it a future-proof solution as it's part of the ECMAScript specification. - **Simplicity**: Syntax is concise and easy to understand. - **Cons**: - **Performance**: In this benchmark, it performs worse than Lodash's method. - **Browser Support**: While fairly modern, older browsers (pre-ES6) do not support the Set object. #### Lodash Approach - **Pros**: - **Performance**: In this benchmark, Lodash's `uniq` method is significantly faster. - **Flexibility**: Lodash provides various utility functions that can simplify complex operations and enhance code readability. - **Cons**: - **Dependency**: Requires including the Lodash library, which could increase the size of your JavaScript bundle if only a single function is used. - **Overhead**: Some developers might consider using a library for simple operations unnecessary, especially if performance is less of a concern. ### Other Considerations - **Alternatives**: - Developers might also consider using other methods for de-duplication, such as `Array.prototype.filter()` combined with `indexOf()`, but these methods typically come with lower performance compared to both the methods evaluated here. - There are also libraries like `underscore.js` that have similar functionality to Lodash, but Lodash is more widely adopted in contemporary JavaScript development. - **Use Cases**: The choice between these two methods may depend on the specific context of an application: - For simpler projects or those where bundle size is critical, the native Set method may be appropriate. - In complex applications that heavily utilize utility functions, the performance benefits of Lodash may outweigh the costs associated with including it. In summary, this benchmark provides insights into the performance of deduplication in JavaScript. The choice of method can depend on factors such as performance requirements, simplicity, and whether or not leveraging a whole library like Lodash makes sense in the context of the application's overall architecture.
Related benchmarks:
lodash uniq vs set
lodash uniq vs set updated lodash
Array lodash uniq vs Set
lodash uniq vs set/array
lodash uniq vs set->array
lodash uniq vs array to set to array
lodash uniq vs set arossert
lodash uniq vs set my
lodash uniq vs set and Array.from
Comments
Confirm delete:
Do you really want to delete benchmark?