Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set union forEach vs forOf
(version: 1)
Comparing performance of:
forEach vs for of vs forEach - no copy vs forOf - no copy
Created:
7 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = new Set(); for (var i = 0; i < 100000; i++) { a.add(i); } var b = new Set(); for (var i = 0; i < 200000; i+= 2) { b.add(i); }
Tests:
forEach
const union = new Set(a); b.forEach(val => union.add(val));
for of
const union = new Set(a); for(const val of b) { union.add(val); }
forEach - no copy
b.forEach(val => a.add(val));
forOf - no copy
for(const val of b) { a.add(val); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
forEach
for of
forEach - no copy
forOf - no copy
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 Set union microbenchmarks. **Benchmark Overview** The benchmark measures the performance difference between two approaches to create a set union in JavaScript: using `forEach` with the `add` method and using `for...of` loop with the same method. The test case uses JavaScript sets, which are implemented as hash tables to store unique values. **Options Compared** There are four options compared: 1. **`forEach` with `add`**: This approach iterates over an array (set) using a traditional `forEach` loop and adds each element to the resulting set using the `add` method. 2. **`for...of` loop with `add`**: Similar to the previous option, but uses a `for...of` loop instead of `forEach`. This approach is more concise and allows for better performance due to the optimized engine for this syntax. 3. **`forEach` without copy**: In this variation, the `forEach` loop does not create a new array (set) by copying the elements, but rather iterates over the original set. The `add` method is still used to add elements to the resulting set. 4. **`for...of` loop without copy**: Similar to the previous option, but uses a `for...of` loop instead of `forEach`. This approach also does not create a new array (set) by copying the elements. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **`forEach` with `add`**: Pros: Easy to understand, widely supported. Cons: May create intermediate arrays or objects, can be slower due to iteration overhead. * **`for...of` loop with `add`**: Pros: More concise, optimized for performance. Cons: Less intuitive for some developers, may require explicit type casting if used in older JavaScript versions. * **`forEach` without copy**: Pros: Reduces memory allocation and garbage collection overhead. Cons: May not be as readable or maintainable due to the lack of intermediate data structures. * **`for...of` loop without copy**: Pros: Similar performance benefits to the previous option, with added clarity due to the explicit iteration syntax. Cons: Still may require explicit type casting if used in older JavaScript versions. **Library Usage** The benchmark uses native JavaScript sets (implemented as hash tables) and arrays (implemented as buffers or arrays). No external libraries are required for this test case. **Special JS Features/Syntax** None of the options mentioned above rely on special JavaScript features like async/await, Promises, or decorators. The `for...of` loop is used in a traditional way to iterate over an iterable object (the set). **Alternatives** Other alternatives for creating sets and performing union operations might include: * Using the `Spread Operator` (`...`) with arrays: This approach can create new sets by spreading array elements into a new set. * Utilizing `Promise.all()` and `Set.from()`: This approach involves using promises to iterate over an iterable object and then converting the results to a set. However, these alternatives might introduce additional overhead or complexity compared to the original benchmark options.
Related benchmarks:
set vs array iteration 100k elements
set vs array iteration new new
3set vs array iteration New doge333
Lodash _.union vs the native Set() for multiple arrays
Comments
Confirm delete:
Do you really want to delete benchmark?