Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Filtering duplicates
(version: 0)
Comparing performance of:
Set + filter vs Reduce vs Set
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; for (let i = 0; i < 100000; i++) { array.push({ id: Math.floor((Math.random() * 10) + 1), name: 'name' + i }); }
Tests:
Set + filter
const seen = new Set(); const filtered = array.filter(el => { const duplicate = seen.has(el.id); seen.add(el.id); return !duplicate; });
Reduce
const filtered = array.reduce((acc, current) => { const x = acc.find(item => item.id === current.id); if (!x) { return acc.concat([current]); } else { return acc; } }, []);
Set
const filtered = Array.from(new Set(array.map(a => a.id))) .map(id => { return array.find(a => a.id === id) })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Set + filter
Reduce
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 world of JavaScript microbenchmarks on MeasureThat.net. The provided benchmark definition json represents a test case that measures the performance of different approaches to remove duplicates from an array of objects. **Benchmark Definition** The script preparation code generates an array of 100,000 objects with random `id` and `name` properties. The purpose is to simulate a scenario where you need to filter out duplicate records. **Options compared** There are three options being compared: 1. **Set + Filter**: This approach uses the `Array.prototype.filter()` method in combination with a `Set` data structure to remove duplicates. The idea is to add each element's `id` to the set and then use the filter method to exclude elements that already exist in the set. 2. **Reduce**: This approach uses the `Array.prototype.reduce()` method to build a new array with only unique elements. It iterates through the original array, finds an existing element by its `id`, and either adds it to the accumulator or skips it. 3. **Set**: This approach uses the `Set` data structure to remove duplicates in one step. It maps each element's `id` to a unique value and then uses `Array.from()` to convert the set back into an array. **Pros and Cons** * **Set + Filter**: * Pros: Simple, readable code, and easy to implement. * Cons: May not be as efficient as other approaches due to the overhead of the filter method. * **Reduce**: * Pros: Efficient use of the reduce method, which can be optimized for performance. * Cons: Code may look more complex or harder to understand for beginners. * **Set**: * Pros: Simple and efficient way to remove duplicates using a set data structure. * Cons: May require additional steps to convert the set back into an array. **Library** None of the provided options use any external libraries. However, some browsers may provide built-in functions or methods that can be used in these approaches (e.g., `Set` objects are supported by most modern browsers). **Special JS feature/syntax** None of the options explicitly use special JavaScript features or syntax. **Other alternatives** There might be other approaches to remove duplicates from an array, such as using `Array.prototype.sort()` and then filtering out consecutive elements. However, these alternatives would likely have different trade-offs in terms of performance, readability, and complexity. Here is a simple implementation for each approach to better illustrate the differences: * **Set + Filter**: ```javascript const seen = new Set(); const filtered = array.filter(el => { const duplicate = seen.has(el.id); seen.add(el.id); return !duplicate; }); ``` * **Reduce**: ```javascript const filtered = array.reduce((acc, current) => { const x = acc.find(item => item.id === current.id); if (!x) { return acc.concat([current]); } else { return acc; } }, []); ``` * **Set**: ```javascript const filtered = Array.from(new Set(array.map(a => a.id))) .map(id => array.find(a => a.id === id)); ```
Related benchmarks:
Filter a list to remove all duplicate values
Methods to remove duplicates from array
Methods to remove duplicates from array (fork)
Methods to remove duplicates from array (test)
Remove duplicates from array of objects
Comments
Confirm delete:
Do you really want to delete benchmark?