Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
returns unique values in array
(version: 0)
Comparing performance of:
_.uniq vs set vs uniq by filter vs Array.from with set
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
Script Preparation code:
var elements = [1,2,3,1,2,4,2,3,5,3]
Tests:
_.uniq
_.uniq(elements)
set
[...new Set(elements)]
uniq by filter
elements.filter((v, i, a) => a.indexOf(v) === i)
Array.from with set
Array.from(new Set(elements))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
_.uniq
set
uniq by filter
Array.from with set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:144.0) Gecko/20100101 Firefox/144.0
Browser/OS:
Firefox 144 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.uniq
11754201.0 Ops/sec
set
4649288.0 Ops/sec
uniq by filter
5715912.0 Ops/sec
Array.from with set
4824437.0 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, compared, and what are the pros and cons of each approach. **Benchmark Overview** The benchmark measures the performance of three different approaches to find unique values in an array: 1. Using the `_.uniq` function from Lodash 2. Creating a new Set from the array using the spread operator (`[...new Set(elements)]`) 3. Filtering the original array using the `indexOf` method and comparing the index with the value **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as data manipulation, string manipulation, and functional programming. In this benchmark, the `_.uniq` function is used to find unique values in an array. The `_` symbol is often referred to as "underscore" and is used to denote a namespace or prefix for Lodash functions. **JavaScript Features/Syntax** The test cases use the following JavaScript features/syntax: * Spread operator (`[...new Set(elements)]`) * Arrow function syntax (`(v, i, a) => a.indexOf(v) === i`) **Approaches Compared** The three approaches compared are: 1. **_.uniq**: This uses the `_.uniq` function from Lodash to find unique values in an array. The implementation is likely to be optimized for performance and might use a hash table or other data structure to achieve this. 2. **Set**: This creates a new Set from the array using the spread operator (`[...new Set(elements)]`). Sets are designed to automatically eliminate duplicates, making this approach efficient but potentially slower than others due to the overhead of creating a new set and converting elements to strings (for hashing). 3. **uniq by filter**: This uses the `indexOf` method to find unique values in an array. The implementation iterates over the original array, keeping track of seen values using the `indexOf` method, which has a time complexity of O(n^2). This approach is likely to be slower than others due to its quadratic time complexity. **Pros and Cons** Here's a brief summary of each approach: * **_.uniq**: Pros: optimized for performance, uses hash table or similar data structure; Cons: relies on Lodash library. * **Set**: Pros: efficient in eliminating duplicates, but creates a new set and converts elements to strings; Cons: potentially slower due to overhead. * **uniq by filter**: Pros: simple and easy to understand; Cons: slow due to quadratic time complexity. **Other Alternatives** Some other approaches that could be used to find unique values in an array include: * Using `Array.prototype.reduce()` or a similar function to accumulate unique values * Using a custom implementation using arrays, maps, or sets * Using a library like `fastest-unique` which provides optimized implementations for finding unique elements Keep in mind that the best approach will depend on the specific use case and performance requirements.
Related benchmarks:
unique elements in array using filter
Lodash - uniq
Lodash - uniq2
lodash vs ES6 uniq
Comments
Confirm delete:
Do you really want to delete benchmark?