Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.uniqWith(arr, _.isEqual).length vs new Set(arr).size 1
(version: 3)
Comparing performance of:
_.uniqWith(arr, _.isEqual).length vs new Set(arr).size
Created:
3 years ago
by:
Registered User
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 arr = Array.from({length: 40}, () => Math.floor(Math.random() * 40)); var newArr = null;
Tests:
_.uniqWith(arr, _.isEqual).length
newArr = _.uniqWith(arr, _.isEqual)
new Set(arr).size
newArr = new Set(arr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.uniqWith(arr, _.isEqual).length
new Set(arr).size
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks and explore what's tested in the provided JSON. **Benchmark Definition** The benchmark is designed to compare two approaches for finding unique elements in an array: 1. Using `_.uniqWith(arr, _.isEqual)` from the Lodash library. 2. Using a built-in JavaScript method `new Set(arr).size`. **Options Compared** These two options are compared because they have different performance characteristics. * `_.uniqWith(arr, _.isEqual)`: This approach uses the Lodash library's `uniqWith` function, which takes a callback function as an argument. In this case, the callback is set to `_.isEqual`, which checks for equality using the `===` operator. This approach is likely to be slower because it involves an additional function call and potentially more memory allocations. * `new Set(arr).size`: This approach uses the built-in JavaScript method `Set`. A `Set` object in JavaScript is a collection of unique values, and its `size` property returns the number of elements in the set. This approach is likely to be faster because it's a native JavaScript method that doesn't involve any additional function calls or memory allocations. **Pros and Cons** Here are some pros and cons of each approach: * `_.uniqWith(arr, _.isEqual)`: + Pros: Easy to implement, readable code. + Cons: Slower performance due to additional function call and potential memory allocations. * `new Set(arr).size`: + Pros: Faster performance, more memory-efficient. + Cons: Requires understanding of the built-in JavaScript method `Set`, potentially harder to implement. **Lodash Library** The Lodash library is a popular utility library for JavaScript that provides a wide range of functions for tasks like array manipulation, string processing, and more. In this case, the `uniqWith` function is used to find unique elements in an array while preserving the original order. **Special JS Feature/Syntax (None)** There are no special JavaScript features or syntaxes being tested in this benchmark. **Other Alternatives** If you're looking for alternative approaches to finding unique elements in an array, here are a few options: * Using `Array.prototype.filter()` and `Array.prototype.indexOf()`: This approach involves filtering the array to find unique elements using `filter()` and then iterating over the resulting array to count the number of unique elements using `indexOf()`. * Using `Array.prototype.reduce()` and `Set`: This approach involves reducing the array to a set of unique values using `reduce()` and then counting the size of the resulting set. * Using `Array.prototype.forEach()` and `Set`: This approach involves iterating over the array using `forEach()` and adding each element to a set, which automatically eliminates duplicates. Here's some sample code for these alternative approaches: ```javascript // Using Array.prototype.filter() and Array.prototype.indexOf() const uniqueElements = arr.filter((element) => { return arr.indexOf(element) === -1; }).length; // Using Array.prototype.reduce() and Set const uniqueElements = arr.reduce((set, element) => { set.add(element); return set.size; }, new Set()).size; // Using Array.prototype.forEach() and Set arr.forEach((element) => { const set = new Set(); set.add(element); // ... }); ``` Note that these alternative approaches may have different performance characteristics than the original benchmark.
Related benchmarks:
Unique lodash vs vanilla
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq vs custom fn
lodash uniq vs Array.from(new Set()) vs spread new Set() [big arrays 2]
Comments
Confirm delete:
Do you really want to delete benchmark?