Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
remove duplicate
(version: 0)
Comparing performance of:
test Set vs test _uniq vs test filter vs object keys
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var array = []; for (let i = 0; i < 100000; i++) { array.push(i % 3); }
Tests:
test Set
const toto = [...new Set(array)];
test _uniq
const tata = _.uniq(array);
test filter
const uniqueArray = array.filter(function(item, pos) { return array.indexOf(item) == pos; })
object keys
const obj = {} for (let i = 0, iLen = array.lenght; i<iLen ; i++){ obj[array[i]] = true } return Object.keys(obj);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
test Set
test _uniq
test filter
object keys
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Android 14; Mobile; rv:135.0) Gecko/135.0 Firefox/135.0
Browser/OS:
Firefox Mobile 135 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
test Set
878.3 Ops/sec
test _uniq
878.6 Ops/sec
test filter
200.5 Ops/sec
object keys
11006351.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, the options compared, pros and cons of each approach, and other considerations. **Benchmark Definition** The benchmark is designed to measure the performance of different approaches for removing duplicates from an array. The `Script Preparation Code` sets up a large array with 100,000 elements, and the `Html Preparation Code` includes a reference to the Lodash library. **Test Cases** There are four test cases: 1. **test Set**: This test uses the built-in JavaScript `Set` object to remove duplicates from the array. 2. **test _uniq**: This test uses the Lodash `_uniq` function to remove duplicates from the array. 3. **test filter**: This test uses a custom implementation of a filter function to remove duplicates from the array. 4. **object keys**: This test uses an object with keys set to unique elements from the original array. **Options Compared** The benchmark compares four different approaches: * `Set`: Built-in JavaScript `Set` object * `_uniq`: Lodash library's `uniq` function * Custom filter implementation: A custom function that filters out duplicates using a simple index check **Pros and Cons of Each Approach** 1. **Set**: Pros: * Fast and efficient * Native to JavaScript, no external dependencies * Simple and intuitive API Cons: * Limited to removing exact duplicates (no handling of partial matches) 2. **_uniq**: Pros: + Robust implementation with support for partial matches and custom sorting + Lodash library provides additional utility functions Cons: + External dependency on Lodash library + May be slower than native `Set` due to overhead of function call 3. Custom filter implementation: Pros: + Customizable to suit specific use cases + No external dependencies Cons: + More complex and error-prone implementation + Slower performance compared to built-in `Set` or `_uniq` **Other Considerations** * The benchmark measures the number of executions per second, which can be affected by factors like CPU frequency, memory allocation, and garbage collection. * The results are reported for each test case separately, allowing users to compare the performance of different approaches on their specific use cases. As for special JS features or syntax, this benchmark does not mention any. However, it's worth noting that some modern browsers may have additional features like Web Workers or async/await support that could impact performance in certain scenarios. **Alternatives** Other alternatives for removing duplicates from an array include: * Using `Map` instead of `Set`: While not as efficient as `Set`, using a `Map` can provide more control over the key type and may be suitable for specific use cases. * Implementing a custom sorting algorithm: This approach would require iterating through the array multiple times to remove duplicates, which could lead to performance issues. * Using a library like `lodash-everything`: While not as lightweight as Lodash's `_uniq`, this library provides an efficient implementation of duplicate removal and other utility functions. Keep in mind that the choice of approach depends on the specific use case, performance requirements, and personal preference.
Related benchmarks:
lodash slice
Clone deep
Comparing lodash's times with Array.from
Spread Operator vs Lodash [2]
Array From vs lodash clone
Comments
Confirm delete:
Do you really want to delete benchmark?