Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Filter for finding duplicates in array of objects
(version: 0)
Set vs Filter for finding duplicates in array of objects
Comparing performance of:
set vs filter
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var deSelectedRows = [{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC01" }, { PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC02" }, { PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC03" }, { PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC04" }]; var selectedRows = [{ PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC01" }, { PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC02" }, { PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC03" }, { PoHeaderKey: 129, OrderNo: "WS1", LineNo: 1, id: "BRIC04" }]
Tests:
set
var ids = new Set(deSelectedRows.map(({ id }) => id)); var final = selectedRows.filter(({ id }) => !ids.has(id)); console.log(final)
filter
var final = selectedRows.filter(function(cv){ return !deSelectedRows.find(function(e){ return e.id == cv.id; }); }); console.log(final);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
set
filter
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 the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is designed to compare two approaches: using a `Set` data structure to find unique elements in an array of objects versus using the `filter()` method with a callback function. **Test Cases** There are two individual test cases: 1. **"set"`**: This test case uses a `Set` data structure to create a set of unique ids from the `deSelectedRows` array, and then filters the `selectedRows` array to exclude rows that exist in the `ids` set. 2. **"filter"`**: This test case uses the `filter()` method with a callback function to filter out rows from `selectedRows` that have an id already present in `deSelectedRows`. **Comparison** The benchmark is comparing the performance of these two approaches: 1. **Set approach**: Using a `Set` data structure allows for efficient lookups and insertions, making it suitable for finding unique elements. 2. **Filter approach**: The `filter()` method with a callback function iterates over the array and checks each element against the set of IDs to determine if it's present or not. **Pros and Cons** Here are some pros and cons of each approach: **Set Approach** Pros: * Efficient lookups and insertions using a hash table * Fast iteration over unique elements Cons: * Requires creating an additional data structure (the `Set` itself) * May have higher memory overhead due to the extra object **Filter Approach** Pros: * No additional data structures are required * Can be implemented with a single array iteration Cons: * Iteration over the entire array for each element can be slow * May involve unnecessary comparisons if the set is large **Library and Special JS Features** In this benchmark, we're using native JavaScript features, such as `Set` objects and the `filter()` method. There are no third-party libraries used in these test cases. **Special JS Feature: Iterators** The `for...of` loop is not explicitly mentioned in the benchmark definition or individual test cases. However, it's worth noting that both approaches can be optimized using iterators (e.g., `Set`'s `forEach()` method or the `filter()` method with an iterator). **Other Alternatives** Some alternative approaches for finding unique elements in an array of objects could include: 1. Using a `Map` data structure instead of `Set`, which allows for efficient lookups and insertions while also providing additional functionality like key-value pairs. 2. Utilizing a library like Lodash's `uniq()` function, which provides a more convenient way to find unique elements without the need for a custom implementation. 3. Implementing a custom iterative solution using techniques like hashing or sorting-based approaches. Keep in mind that these alternatives may have varying performance characteristics and memory overhead compared to the original set approach.
Related benchmarks:
Deduplicate array test
Deduplicate array test v2
lodash UniqueWith vs custom filter to remove duplicates
lodash UniqueWith vs custom filter with isEqual for duplicates
lodash uniq vs filter vs set
Comments
Confirm delete:
Do you really want to delete benchmark?