Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash uniq vs set copy to array
(version: 1)
Comparing performance of:
Native vs lodash uniq
Created:
6 months 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:
var obj = [1, 2, 3, 4, 6, 6, 4, 1, 2, 2, 3];
Tests:
Native
let u = [...new Set(obj)];
lodash uniq
let u = _.uniq(obj);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Native
lodash uniq
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Browser/OS:
Chrome 141 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Native
12950323.0 Ops/sec
lodash uniq
36923632.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 6 months ago):
The benchmark represented in the provided JSON compares two methods of producing a unique array from a given array of numbers: using native JavaScript features and using the Lodash library. ### Options Compared 1. **Native Method: `let u = [...new Set(obj)];`** - This approach leverages the `Set` object, a built-in data structure in JavaScript that stores unique values. The spread operator (`...`) is used to expand the set back into an array. 2. **Lodash Method: `let u = _.uniq(obj);`** - This approach utilizes Lodash’s `uniq` function, which is part of the Lodash library. Lodash is a widely used utility library that provides many functions for common programming tasks, including working with arrays, objects, and strings. The `uniq` function takes an array and returns a new array with duplicate values removed. ### Pros and Cons #### Native Method: - **Pros:** - **Performance:** The results show significantly better performance in this case, with `ExecutionsPerSecond` of 36,923,632, indicating it's faster for this specific use case. - **Simplicity:** The syntax is straightforward and does not require any external libraries, making it easy to understand. - **Modern JavaScript:** Utilizing modern features like `Set` and the spread operator is generally encouraged in current JavaScript coding practices. - **Cons:** - **Browser Support:** While largely supported in modern browsers, using `Set` and the spread operator may not be ideal for legacy environments that don’t support these features. #### Lodash Method: - **Pros:** - **Readability:** Many developers find Lodash functions to be more readable and expressive. `_.uniq()` clearly communicates the intent to retrieve unique values. - **Consistency:** If the project already uses Lodash, leveraging its functions can maintain consistency throughout the codebase. - **Feature-Rich:** Lodash can handle more complex structures and conditions that the native method might not cover, such as deeply nested objects or specific comparator functions. - **Cons:** - **Performance:** As seen in the benchmark results, `_.uniq(obj)` had a significantly lower performance at 12,950,323 `ExecutionsPerSecond`, making it less suitable for performance-critical applications. - **Dependency:** Using Lodash introduces an additional dependency into the project, which may be unnecessary if only simple utility functions are used. ### Other Considerations - **Use Case Suitability:** The choice between these methods may depend significantly on the specific requirements of the project, such as performance needs, coding style preferences, and whether or not Lodash is already in use. - **Alternative Methods:** Other alternatives for achieving unique values in an array could include: - **Filtering with `indexOf`:** Loop through the array, pushing values to a new array only if they haven't been encountered before. - **Using `reduce`:** The reduce method can create a unique array by building it incrementally. - **External Libraries:** There are other utility libraries with similar functionality, such as Underscore.js, which could be considered if more functionality is required. In summary, while the native approach is faster and doesn't require extra libraries, the Lodash approach may offer readability and consistency for projects that already adopt the library. Choosing the right method depends on the specific context and constraints of the software project.
Related benchmarks:
lodash uniq vs native uniq
Lodash uniqBy vs Set with Array of object
lodash uniq vs set - 3
Lodash uniqBy vs Set vs Set spread
lodash uniq vs spread new Set() medium size
Lodash uniqWith vs Set
Lodash uniqBy vs Set with Big array
lodash uniq vs spread new Set() vs Array.from(new Set) medium size
Lodash uniqBy vs Set, big array
Comments
Confirm delete:
Do you really want to delete benchmark?