Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Fastest difference between multiple sets
(version: 0)
Comparing performance of:
setsDifference(from, sets) vs setsDifference(from, setsUnion(sets)) vs setsDifferenceModify(from, sets) vs setsDifferenceModify(from, setsUnion(sets))
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var setsUnion = (sets) => { if (sets.length === 1) { return sets[0]; } const result = new Set(); for (const set of sets) { for (const item of set) { result.add(item); } } return result; }; var setsDifference = (from, sets) => { const result = new Set(); for (const value of from) { let add = true; for (const set of sets) { if (set.has(value)) { add = false; break; } } if (add) { result.add(value); } } return result; }; var setsDifference2Args = (from, set) => { const result = new Set(); for (const value of from) { let add = true; if (set.has(value)) { add = false; break; } if (add) { result.add(value); } } return result; }; var setsDifferenceModify = (from, sets) => { for (const value of from) { for (const set of sets) { if (set.has(value)) { from.delete(value); break; } } } }; var setsDifferenceModify2Args = (from, set) => { for (const value of from) { if (set.has(value)) { from.delete(value); break; } } }; var from = new Set([3, 5, 1, 8, 4, 10, 46, 33, 21, 345, 14, 72, 5, 3, 6, 7, 8, 9, 10]); var sets = [ new Set([9, 10, 46, 21, 66, 100, 200, 300, 400, 2]), new Set([6, 15, 22, 2, 10, 66, 1, 0, 900]), new Set([22, 15, 21, 4, 1, 3, 0, 200]) ];
Tests:
setsDifference(from, sets)
const result = setsDifference(from, sets);
setsDifference(from, setsUnion(sets))
const result = setsDifference2Args(from, setsUnion(sets));
setsDifferenceModify(from, sets)
setsDifferenceModify(from, sets);
setsDifferenceModify(from, setsUnion(sets))
setsDifferenceModify2Args(from, setsUnion(sets));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
setsDifference(from, sets)
setsDifference(from, setsUnion(sets))
setsDifferenceModify(from, sets)
setsDifferenceModify(from, setsUnion(sets))
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 benchmark and explain what's being tested. **Benchmark Overview** The benchmark is testing four different approaches for calculating the difference between two sets in JavaScript. The tests are designed to measure which approach is the fastest, with a focus on the "setsDifferenceModify" variant. **Approaches Compared** 1. **`setsDifference(from, sets)`**: This function iterates over each element in `from` and checks if it exists in any of the sets in `sets`. If an element is not found, it's added to the result set. 2. **`setsDifference2Args(from, setsUnion(sets))`**: This function uses a union function (`setsUnion`) to first combine all the sets in `sets` into a single set. Then, it iterates over each element in `from` and checks if it exists in the combined set. If an element is not found, it's added to the result set. 3. **`setsDifferenceModify(from, sets)`**: This function modifies the original `from` set by deleting elements that exist in any of the sets in `sets`. The result is then returned as a new set containing only the elements that are present in `from` but not in `sets`. 4. **`setsDifferenceModify2Args(from, setsUnion(sets))`**: This function uses the union function (`setsUnion`) to combine all the sets in `sets` into a single set. Then, it iterates over each element in `from` and deletes it from the result set if it exists in the combined set. **Pros and Cons of Each Approach** 1. **`setsDifference(from, sets)`**: This approach has the highest overhead due to the need to check for presence in multiple sets. However, it's simple and easy to implement. 2. **`setsDifference2Args(from, setsUnion(sets))`**: This approach uses a union function, which adds complexity but can be faster if the sets are large and there are many duplicate elements. 3. **`setsDifferenceModify(from, sets)`**: This approach modifies the original set in place, making it very efficient but also potentially less predictable. 4. **`setsDifferenceModify2Args(from, setsUnion(sets))`**: This approach uses a union function and deletes elements from `from`, making it more complex than `setsDifference` but still relatively fast. **Library Used** The benchmark doesn't explicitly mention any libraries used, but the presence of the `Set` data structure suggests that JavaScript's built-in set implementation is being tested. The use of a union function (`setsUnion`) implies that the benchmark is using a library or a custom implementation to combine sets. **Special JS Feature/ Syntax** There are no special JavaScript features or syntax mentioned in the benchmark, but the use of closures and higher-order functions (e.g., `setsDifference2Args` and `setsDifferenceModify2Args`) suggests a moderate level of complexity in the codebase. **Alternatives** Other approaches for calculating set difference could include: * Using a different data structure, such as an array or a hash map. * Implementing a custom algorithm that takes advantage of specific properties of sets (e.g., symmetry or idempotence). * Using parallel processing or multi-threading to take advantage of multiple CPU cores. However, the benchmark's focus on testing different implementations of set difference using JavaScript's built-in data structures and functions makes it a good testbed for evaluating performance optimizations.
Related benchmarks:
convert to set + set.has vs. array.includes
loop over Set conditionally vs non-conditionally
set vs array iteration 100k elements
Set vs Filter vs forEach vs forLoop for unique
Comments
Confirm delete:
Do you really want to delete benchmark?