Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Unique values with Lodash
(version: 0)
Comparing performance of:
Set vs Includes vs Lodash uniq with spread
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Tests:
Set
const fn = (existing, newVal) => { return [...new Set([...existing, newVal])]; } fn([1, 2, 3], 1)
Includes
const fn = (existing, newVal) => { if (existing.includes(newVal)) { return existing } return [...new Set([...existing, newVal])]; } fn([1, 2, 3], 1)
Lodash uniq with spread
const fn = (existing, newVal) => { return _.uniq([...existing, newVal]) } fn([1, 2, 3], 1)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Set
Includes
Lodash uniq with spread
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 provided benchmarking test. **Benchmark Overview** The test is designed to measure the performance of creating a set (a collection of unique values) in JavaScript using three different approaches: using `Set` objects, checking for inclusivity with the `includes()` method, and using Lodash's `uniq()` function with spread syntax. **Approach 1: Using `Set` Objects** This approach creates a new set by taking the union of two arrays using the spread operator (`[...existing, newVal]`) and then wrapping it in a `Set` object. The test case uses this approach to create a set from an existing array `[1, 2, 3]` with a new value `1`. **Pros:** * Fast and efficient way to create unique values * Native JavaScript implementation **Cons:** * May have higher memory overhead due to the creation of a new set object * Could be slower if the input arrays are large due to the additional overhead of creating a new set **Approach 2: Checking for Inclusivity with `includes()` Method** This approach checks if the new value is already present in the existing array using the `includes()` method. If it's not present, it adds the new value to the array and returns the updated array. The test case uses this approach to create a set from an existing array `[1, 2, 3]` with a new value `1`. **Pros:** * May be faster than using `Set` objects for large input arrays * No additional memory overhead **Cons:** * Slower than using `Set` objects due to the extra check and potential array resizing * Inefficient if the existing array is very large, as it would require a linear search **Approach 3: Lodash's `uniq()` Function with Spread Syntax** This approach uses Lodash's `uniq()` function to remove duplicates from the input array. The test case uses this approach to create a set from an existing array `[1, 2, 3]` with a new value `1`. **Pros:** * Fast and efficient way to remove duplicates * No additional memory overhead **Cons:** * Requires Lodash library to be loaded * May have higher performance overhead due to the need for a function call and potential library loading delay **Library and Syntax** The test case uses Lodash version 4.17.4, which is an external JavaScript library that provides a set of utility functions. The `uniq()` function with spread syntax is used in this approach. **Special Features or Syntax** None mentioned. **Alternative Approaches** Other approaches to creating unique values in JavaScript include: 1. Using `Array.prototype.filter()` method: This approach filters out duplicate values using the filter() method, which can be slower than using a set object. 2. Using `Map` objects: This approach uses a Map object to store unique values, which can be slower than using a set object due to the additional memory overhead. Overall, the test highlights the importance of choosing an efficient and suitable approach for creating unique values in JavaScript, taking into account factors such as performance, memory usage, and library dependencies.
Related benchmarks:
uniqBy performance
uniqBy vs stringify performance
Lodash - uniq
lodash vs ES6 uniq
Lodash uniqBy vs Javascript uniqBy
Comments
Confirm delete:
Do you really want to delete benchmark?