Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash#uniq + Native v2
(version: 0)
Comparing performance of:
_.uniqWith vs Using set vs Using Array#includes
Created:
3 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>
Script Preparation code:
var list = new Array(10000) for (let i = 0; i < list.length; ++i) { list[i] = { v: Math.ceil(100 * Math.random()) } } function getValue(v) { return v.v } function comparator(v1, v2) { return v1.v === v2.v } function deduplicateWithSet(l) { const s = new Set() return l.filter(v => { const vv = getValue(v) if (s.has(vv)) return false s.add(vv) return true }) } function deduplicateWithIncludes(l) { const s = [] return l.filter(v => { const vv = getValue(v) if (s.includes(vv)) return false s.push(vv) return true }) }
Tests:
_.uniqWith
return _.uniqWith(list, comparator);
Using set
return deduplicateWithSet(list)
Using Array#includes
return deduplicateWithIncludes(list)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.uniqWith
Using set
Using Array#includes
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 break down what is being tested on the provided JSON benchmark. **Benchmark Definition** The script preparation code generates an array of 10,000 objects with a random `v` property. The script defines three functions: 1. `getValue(v)`: returns the value of the `v` property. 2. `comparator(v1, v2)`: compares two values using the `===` operator. 3. Two functions that attempt to deduplicate the array: * `deduplicateWithSet(l)`: uses a Set to keep track of unique values. * `deduplicateWithIncludes(l)`: uses Array#includes to check for duplicate values. **Options Compared** The benchmark compares three approaches: 1. **_.uniqWith(list, comparator)**: uses Lodash's `uniqWith` function, which takes a comparator function as an argument. This approach is likely to be the most efficient, as it leverages optimized C code under the hood. 2. **Using set**: uses a Set to keep track of unique values. This approach has a time complexity of O(n), making it potentially slower than Lodash's implementation. 3. **Using Array#includes**: uses Array#includes to check for duplicate values. This approach also has a time complexity of O(n), but may be slower due to the overhead of calling `includes` on each iteration. **Pros and Cons** * **_.uniqWith(list, comparator)**: + Pros: likely to be the most efficient, leverages optimized C code. + Cons: depends on Lodash's implementation, may not provide a clear understanding of the underlying algorithm. * **Using set**: + Pros: simple and easy to understand, can be faster than Array#includes if implemented correctly. + Cons: time complexity is O(n), may be slower for large datasets. * **Using Array#includes**: + Pros: easy to implement and understand, can provide a good benchmarking baseline. + Cons: time complexity is O(n), may be slower due to the overhead of calling `includes` on each iteration. **Library and Its Purpose** The benchmark uses Lodash, a popular JavaScript utility library. The `uniqWith` function takes a comparator function as an argument, allowing users to customize the comparison logic. **Special JS Features or Syntax** There are no special JS features or syntax mentioned in the benchmark definition. However, it's worth noting that the use of Set and Array#includes demonstrates good understanding of JavaScript's built-in data structures and algorithms. **Other Alternatives** If you're interested in exploring alternative approaches, here are some suggestions: * Use a different comparison function instead of `===`. * Implement your own deduplication algorithm using bitwise operations or bit manipulation. * Compare the performance of using a Trie data structure for deduplication. * Experiment with different caching strategies to improve performance.
Related benchmarks:
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq vs custom fn
Create an array with unique values - Javascript Array.reduce/Array.indexOf vs Lodash Uniq vs custom fn vs custom2
Uniq by vs Array
Set vs Filter vs _.uniq for unique
Comments
Confirm delete:
Do you really want to delete benchmark?