Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniq vs set incl. destructuring
(version: 0)
Comparing performance of:
Set vs Array
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script>
Tests:
Set
var l = [...new Set([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7])]; return l;
Array
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
Array
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
6010106.0 Ops/sec
Array
10272435.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. The provided benchmark definition json shows that we have two test cases: `Set` and `Array`. Both test cases aim to measure the performance difference between using a native JavaScript approach (`Set`) versus a library-based approach (`_.uniq(l)` from Lodash) for removing duplicate elements from an array. **Native Approach (Set)** The first test case uses a native JavaScript `Set` object to remove duplicates. A `Set` is a collection of unique values, which is exactly what we need here. When we create a new `Set` from the array `[1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]`, JavaScript automatically removes any duplicate elements. The resulting Set object is then converted back to an array and returned. **Library-Based Approach (Array)** The second test case uses the `_.uniq` function from Lodash to remove duplicates from the same array. Lodash is a popular JavaScript library that provides various utility functions, including `_.uniq`, which returns a new array with unique elements. **Options Compared** In this benchmark, we have two options: 1. **Native Approach (Set)**: This approach uses a native JavaScript object (`Set`) to remove duplicates. 2. **Library-Based Approach (Array)**: This approach uses the `_.uniq` function from Lodash to remove duplicates. **Pros and Cons of Each Approach** * **Native Approach (Set)**: + Pros: - Built-in JavaScript feature, no external dependencies - Can be faster since it doesn't involve a library call + Cons: - May not work in older browsers or environments that don't support `Set` - Requires more manual effort to implement and test * **Library-Based Approach (Array)**: + Pros: - Widely supported across different browsers and environments - Easier to implement and test since it's a built-in library function + Cons: - Involves an external dependency (Lodash) - May be slower due to the overhead of the library call **Other Considerations** * **Browser Support**: The `Set` object is supported in modern browsers, while older browsers may not support it. Lodash, on the other hand, has excellent browser support. * **Performance**: Measuring performance can be complex and depends on various factors, such as the specific use case, hardware, and software environment. **Special JS Feature or Syntax** There are no special JavaScript features or syntaxes used in this benchmark. It's a straightforward comparison of two approaches using native JavaScript and Lodash. **Alternative Approaches** If you're interested in exploring alternative approaches, here are some options: * **Other libraries**: Other libraries like underscore.js or moment.js have similar functions for removing duplicates. * **Native alternatives**: If you prefer not to use `Set`, you could use other native JavaScript objects like `Array.prototype.filter()` or `Array.prototype.reduce()`. * **ES6+ features**: With the advent of ES6+ features, you might consider using modern JavaScript features like `Map` or `WeakSet` for removing duplicates. In conclusion, this benchmark provides a clear comparison between two approaches for removing duplicates from an array: the native JavaScript approach using `Set`, and the library-based approach using Lodash's `_.uniq` function.
Related benchmarks:
lodash uniq vs vanilla set - final
lodash uniq vs native uniq
uniqBy vs stringify performance
Lodash uniqBy vs Native
Lodash uniqBy vs Javascript uniqBy
Comments
Confirm delete:
Do you really want to delete benchmark?