Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash uniq vs array to set to array
(version: 0)
Comparing performance of:
Set vs Array
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js'></script>
Tests:
Set
var l = [...new Set([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7])]; return l;
Array
var l = [1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]; return _.uniq(l);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set
Array
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0
Browser/OS:
Chrome 122 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
2478265.0 Ops/sec
Array
4151068.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is tested, the options compared, their pros and cons, and other considerations. **Benchmark Overview** The benchmark tests two approaches for removing duplicates from an array: using JavaScript's built-in `Set` object and using the popular utility library Lodash's `uniq` function. **Test Cases** There are only two test cases: 1. **Set**: This test case uses JavaScript's built-in `Set` object to remove duplicates from an array. 2. **Array**: This test case uses Lodash's `uniq` function to remove duplicates from an array. **Options Compared** The benchmark compares the performance of two approaches: 1. **JavaScript Set**: Using a `Set` object to convert the array to a set (which automatically removes duplicates) and then converting it back to an array. 2. **Lodash uniq**: Using Lodash's `uniq` function to remove duplicates from the original array. **Pros and Cons** Here are some pros and cons of each approach: 1. **JavaScript Set**: * Pros: + Fast and efficient, as it uses a hash table under the hood. + No dependencies on external libraries. * Cons: + May not work correctly for arrays with non-numeric keys (e.g., objects). + Converts the array to a set, which may incur additional memory overhead. 2. **Lodash uniq**: * Pros: + Works correctly for arrays with non-numeric keys. + Does not require converting the array to a new data structure. * Cons: + May be slower than the JavaScript Set approach due to additional overhead from the library. **Library: Lodash** Lodash is a popular utility library that provides a wide range of functions for common tasks, such as string manipulation, object manipulation, and array manipulation. The `uniq` function in particular is designed to remove duplicates from an array while preserving the original order. **Special JS Feature/Syntax: None** This benchmark does not use any special JavaScript features or syntax beyond what is standard in modern JavaScript implementations. **Other Alternatives** If you're looking for alternative approaches, here are a few: 1. **Array.prototype.filter**: You can use `Array.prototype.filter` to remove duplicates from an array by comparing each element with every other element. 2. **Array.prototype.reduce**: You can use `Array.prototype.reduce` to group elements by their values and then return the unique groups. Here's some sample code using these alternative approaches: ```javascript // Using Array.prototype.filter: var l = [...new Set([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7])]].filter((x, i) => new Set([...l]).has(x)); ``` ```javascript // Using Array.prototype.reduce: var l = [...new Set([1, 2, 3, 4, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7])]].reduce((groups, x) => { if (!groups.find(g => g[0] === x)) { groups.push([x]); } return groups; }, []); ``` Keep in mind that these alternatives may have different performance characteristics compared to the original approaches tested by the benchmark.
Related benchmarks:
lodash uniq vs native uniq
uniqBy vs stringify performance
lodash uniq vs set - 3
Lodash uniqBy vs Javascript uniqBy
lodash uniq vs spread new Set() medium size
Comments
Confirm delete:
Do you really want to delete benchmark?