Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array of strings, null, and ints lodash uniq vs set
(version: 0)
What is the fastest method of getting a the unique items from an array
Comparing performance of:
Set vs uniq
Created:
2 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script>
Script Preparation code:
var arr = ['Batman', 'Dr. Doom', null, 'Iron Man', 'Rorschach', 'Captain America', 'J. Jonah Jameson', 'The Mekon', null, '', null, '', 'Scott Pilgrim', 'Batman', 'Dr. Doom', 'Iron Man', 'Rorschach', 'Captain America', 'J. Jonah Jameson', 'The Mekon', 'Scott Pilgrim', 1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7];
Tests:
Set
var u = new Set(arr); return u;
uniq
var u = _.uniq(arr) return u;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set
uniq
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
2562858.2 Ops/sec
uniq
1917114.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON to understand what's being tested. **Benchmark Definition:** The benchmark tests two approaches to get unique items from an array: 1. Using a `Set` data structure ( Benchmark Definition: "var u = new Set(arr);\r\nreturn u;" ) 2. Using the Lodash library's `uniq` function ( Benchmark Definition: "var u = _.uniq(arr)\r\nreturn u;" ) **What is tested?** The benchmark compares the performance of these two approaches in two aspects: 1. **Memory usage**: How much memory does each approach allocate for the resulting set or array? 2. **Execution time**: How fast are each approach in returning the unique items? **Options compared:** * Set data structure ( native JavaScript ) * Lodash library's `uniq` function **Pros and cons of each approach:** 1. **Set data structure ( native JavaScript )** * Pros: + Fast execution time, as it uses a hash table under the hood. + Low memory usage, as it only stores unique values. + No dependencies on external libraries. * Cons: + May not be suitable for large datasets, as it can lead to a larger memory allocation. 2. **Lodash library's `uniq` function** * Pros: + Easy to use and understand, especially for developers familiar with Lodash. + Can handle large datasets without significant performance degradation. * Cons: + Higher memory usage compared to the Set data structure approach. + Dependent on an external library (Lodash), which may not be suitable for all projects. **Library:** The `uniq` function is part of the Lodash library, a popular JavaScript utility library that provides a wide range of functions for various tasks, such as array manipulation, string manipulation, and more. **Special JS feature or syntax:** None mentioned in this benchmark. The use of `Set` data structure is a built-in JavaScript feature, while the `uniq` function from Lodash is a specific implementation of an algorithm to remove duplicates from an array. **Other alternatives:** If you want to explore other approaches, here are a few options: 1. Using the `Array.prototype.filter()` method and `Array.from()`: `const u = Array.from(arr).filter((value, index, self) => self.indexOf(value) === index);` 2. Using the `Array.prototype.reduce()` method: `const u = arr.reduce((unique, value) => { if (!unique.has(value)) unique.add(value); return unique; }, new Set());` 3. Using a custom implementation without relying on external libraries: This approach would require implementing the logic to remove duplicates from an array. These alternatives may have different performance characteristics and trade-offs compared to the Set data structure and Lodash's `uniq` function.
Related benchmarks:
Lodash uniq vs Set to unique array
Lodash unique vs Array.from(new Set)
JS fastest unique array Set vs uniq vs filter
lodash uniq vs set lodash@4.17.21
Comments
Confirm delete:
Do you really want to delete benchmark?