Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
New set vs UniqWith
(version: 0)
Comparing performance of:
uniqWith vs New Set
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:
var MyArr = Array.from({length: 10000}, () => Math.floor(Math.random() * 40)); var myCopy = null;
Tests:
uniqWith
myCopy = _.uniqWith(MyArr);
New Set
myCopy = [...new Set(MyArr)]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
uniqWith
New Set
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/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
uniqWith
5682.7 Ops/sec
New Set
5662.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare two approaches for removing duplicates from an array: `uniqWith` from the Lodash library, and the built-in `Set` data structure in JavaScript (via the `New Set` approach). **Script Preparation Code** The script preparation code creates a large random array `MyArr` with 10,000 elements. It also initializes a variable `myCopy` to null. **Html Preparation Code** The HTML preparation code includes a reference to the Lodash library (version 4.17.5) in the format `https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js`. **Benchmark Definitions** There are two benchmark definitions: 1. **`myCopy = _.uniqWith(MyArr)`**: This line uses the `uniqWith` function from Lodash to remove duplicates from the `MyArr` array. The `_` symbol is a reference to the Lodash library, which provides this utility function. 2. **`myCopy = [...new Set(MyArr)]`**: This line creates a new `Set` object from the `MyArr` array and then uses the spread operator (`...`) to create a new array containing only the unique elements. **Comparison** The two approaches are being compared in terms of their performance, measured by the number of executions per second. **Pros and Cons:** 1. **`uniqWith`**: This approach uses a third-party library (Lodash) and provides a more concise way to remove duplicates. However, it may introduce additional overhead due to the library's presence. * Pros: Efficient and easy to use. * Cons: Requires an external dependency, which might not be desirable for all projects. 2. **`New Set`**: This approach uses the built-in `Set` data structure in JavaScript, which is a lightweight and efficient way to remove duplicates. * Pros: Lightweight, fast, and doesn't require an external library. * Cons: Can be less concise than using `uniqWith`. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, string manipulation, and more. In this benchmark, the `_` symbol refers to the Lodash library, which includes the `uniqWith` function. **Special JS Feature/ Syntax: None** There are no special JavaScript features or syntax used in this benchmark that would require explanation. **Other Alternatives:** 1. **Using Array.prototype.filter()**: Another way to remove duplicates from an array is by using the `filter()` method with a callback function. 2. **Using Map**: Creating a new `Map` object and adding elements to it can also be used to remove duplicates, as `Map` automatically eliminates duplicate keys. Here's an example of how you could rewrite the benchmark using `Array.prototype.filter()`: ```javascript myCopy = MyArr.filter((element, index) => { return MyArr.indexOf(element) === index; }); ``` And here's an example of how you could use a `Map` to remove duplicates: ```javascript const map = new Map(); MyArr.forEach(element => map.set(element, true)); myCopy = Array.from(map.keys()); ```
Related benchmarks:
Lodash uniqBy vs Set
Lodash uniqBy vs Set 10000
Unique lodash vs vanilla
Lodash uniqBy vs Set vs Set spread
Comments
Confirm delete:
Do you really want to delete benchmark?