Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set vs lodash uniq vs custom unique 2
(version: 1)
Comparing performance of:
Set vs Lodash uniq vs Custom unique
Created:
one year ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js'></script>
Script Preparation code:
function unique( array, equals = Object.is, ) { const result = array.filter( (item, index, array) => array.findIndex((a) => equals(a, item)) === index, ); return result.length === array.length ? array : result; }
Tests:
Set
const l = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; return new Set(l);
Lodash uniq
const l = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; return _.uniq(l);
Custom unique
const l = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; return unique(l);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Set
Lodash uniq
Custom unique
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
4358537.0 Ops/sec
Lodash uniq
6632406.0 Ops/sec
Custom unique
4498624.5 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. **Benchmark Overview** The provided benchmark compares three approaches to remove duplicates from an array: using the built-in `Set` object, Lodash's `uniq` function, and a custom implementation called `unique`. **Options Compared** 1. **Built-in Set Object**: This approach uses the `Set` object to remove duplicates from the array. 2. **Lodash uniq**: This approach uses Lodash's `uniq` function, which is a utility function for removing duplicates from an array. 3. **Custom Unique Function**: This approach defines a custom `unique` function using JavaScript's `filter` method and the `findIndex` method. **Pros and Cons** 1. **Built-in Set Object**: * Pros: Fast and efficient, as it uses a hash table to store unique elements. * Cons: May not work well with non-numeric or non-string values, and may have performance issues with very large datasets. 2. **Lodash uniq**: * Pros: Robust and feature-rich, with support for various types of duplicates (e.g., array vs. object). * Cons: Adds an external dependency (Lodash), which may increase load times or bundle sizes. 3. **Custom Unique Function**: * Pros: Customizable and flexible, allowing developers to optimize performance or handle specific edge cases. * Cons: May have higher overhead due to the custom implementation, potentially affecting performance. **Library and Features** 1. **Lodash uniq**: Lodash is a popular utility library for JavaScript that provides a wide range of functions for data manipulation, including `uniq`. 2. **Set Object**: The built-in Set object in JavaScript is used to store unique values, making it an efficient choice for removing duplicates. **Special JS Features or Syntax** None are mentioned in the benchmark definition or individual test cases. **Other Alternatives** 1. **Array.prototype.filter()**: This method can be used to remove duplicates from an array using a custom implementation. 2. **Reduce() and Array.prototype.indexOf()**: Another approach to removing duplicates could involve using `reduce()` to iterate over the array and `indexOf()` to check for duplicate values. In summary, the benchmark compares three approaches to remove duplicates from an array: using the built-in Set object, Lodash's uniq function, and a custom implementation. Each approach has its pros and cons, and understanding these differences can help developers choose the best solution for their specific use case.
Related benchmarks:
Lodash uniq vs Set to unique array
lodash uniq vs set - 3
lodash uniqBy vs custom uniqBy
lodash@4.17.21 uniq vs set vs custom unique
Comments
Confirm delete:
Do you really want to delete benchmark?