Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
unique elements in array using filter fork2
(version: 0)
Comparing performance of:
_.uniq vs set vs uniq by filter vs uniq by qiankun vs uniq by filter & 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)
uniq by qiankun
elements.filter(function filter(element) { return element in this ? false : ((this)[element] = true); }, Object.create(null));
uniq by filter & set
const seen = new Set(); elements.filter(( x) => { if(seen.has(x)) return false; seen.add(x); return true });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
_.uniq
set
uniq by filter
uniq by qiankun
uniq by filter & set
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 dive into the benchmark. The test case measures the performance of different approaches to remove duplicate elements from an array. The arrays in question are generated with duplicates, making it easier to identify the differences between the implementations. Here's a brief explanation of each approach: 1. **_.uniq** (Lodash function): This implementation uses Lodash's `uniq` function, which returns a new array with unique values. 2. **set**: This implementation uses the built-in `Set` object in JavaScript, which automatically removes duplicates when creating a new set from an array. 3. **uniq by filter** (using Array.prototype.filter() and Array.prototype.indexOf()): This approach creates a new array with only the first occurrence of each element in the original array. It works by checking if the current index `i` is equal to the index of the value `v` using `Array.prototype.indexOf()`. 4. **uniq by qiankun** (using a custom implementation): This approach uses a similar idea as the previous one but implements it manually without relying on built-in methods. 5. **uniq by filter & set**: This approach combines the ideas from the first two implementations, using both filtering and a `Set` to remove duplicates. Now, let's discuss the pros and cons of each approach: * **_.uniq**: + Pros: Highly optimized, efficient, and widely supported. + Cons: Introduces additional dependencies (Lodash). * **set**: + Pros: Fast, lightweight, and built-in to JavaScript. + Cons: Requires creating a new set object, which can be slower for large arrays. * **uniq by filter & set**: + Pros: Combines the benefits of both approaches, providing an efficient and lightweight solution. + Cons: Adds complexity due to using two methods. * **uniq by qiankun**: + Pros: Manually optimized, potentially faster than other approaches for very large arrays. + Cons: Requires manual implementation and potential errors. Other considerations: * The custom implementation in the "uniq by qiankun" approach might be more efficient due to its low-level optimization. However, it also comes with a higher risk of introducing bugs or performance regressions if not implemented correctly. * Using built-in methods like `Set` is generally recommended, but for very large arrays, the custom implementation might provide better performance. The benchmark results show that: * "uniq by filter" and "set" are close in terms of execution speed, with "set" being slightly faster due to its lower overhead. * The custom implementation ("uniq by qiankun") provides a slower but potentially more efficient solution for very large arrays. * ".uniq" is the slowest approach, likely due to the additional dependency and optimization costs. Keep in mind that these results are specific to this benchmark and might not generalize to other use cases. It's essential to consider the trade-offs between performance, readability, and maintainability when choosing an implementation.
Related benchmarks:
unique elements in array using filter
unique elements in array using filter - lodash 4.17.21
Lodash - uniq2
unique elements in array using filter fork
Comments
Confirm delete:
Do you really want to delete benchmark?