Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set vs array iteration + for each - large arrays - sum
(version: 0)
Comparing performance of:
array vs set vs Array for Each vs Set for Each
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const base = Array.from(Array(10000).keys()) var a = base.map(Math.random); var b = new Set(base.map(Math.random)) var c = new Set(base.map(Math.random)) var d = base.map(Math.random);
Tests:
array
let sum = 0 for (const x of a) {sum += x}
set
let sum = 0 for (const x of b) {sum += x}
Array for Each
let sum = 0 d.forEach(x => sum += x)
Set for Each
let sum = 0 c.forEach(x => sum += x)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
array
set
Array for Each
Set for Each
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):
I'd be happy to explain what's being tested in the provided benchmark. **Benchmark Overview** The test cases are designed to measure the performance of three different approaches: 1. **Array Iteration**: This approach uses a `for...of` loop to iterate over an array and perform an operation (in this case, summing up the elements). 2. **Set Iteration**: Similar to array iteration, but using a `Set` data structure instead. 3. **For Each on Array and Set**: These tests use the `forEach` method to iterate over arrays and sets, respectively. **Options Compared** The benchmark compares two options for each approach: * **Array Iteration**: Using a plain JavaScript array (`base`) versus using the `map` function to create an iterator over the array elements. * **Set Iteration**: Using a `Set` object directly versus using the `map` function to create an iterator over the set elements. **Pros and Cons** 1. **Array Iteration**: * Pros: Simple, straightforward approach that works well for many use cases. * Cons: Can be slower than other approaches due to the overhead of creating an array iterator. 2. **Set Iteration**: * Pros: Fast and efficient way to iterate over sets, as it avoids the overhead of checking membership in a set. * Cons: May not work well for all use cases, such as when the set contains duplicate elements or is very large. 3. **For Each on Array and Set**: * Pros: Can be faster than explicit iteration methods like `forEach` due to the optimized implementation under the hood. * Cons: May not be suitable for all use cases, such as when the array or set needs to be modified during iteration. **Library Used** None explicitly mentioned in the benchmark code. However, the `Set` data structure is used, which is a built-in JavaScript API. **Special JS Feature/Syntax** The tests do not rely on any special JavaScript features or syntax beyond what's commonly supported by modern browsers. **Other Alternatives** If you're interested in exploring other approaches, here are some alternatives to consider: * Using `Promise.all` instead of explicit iteration methods for array and set iterations. * Utilizing `Map` data structures for more efficient lookups and operations on large datasets. * Leveraging WebAssembly (WASM) or other low-level optimizations for performance-critical code. Keep in mind that these alternatives may not be relevant to the specific use case presented by this benchmark, and their effectiveness depends on various factors such as hardware, software configurations, and algorithmic choices.
Related benchmarks:
map.has vs. array.includes - large array - random Access
set vs array iteration 100k elements
set vs array iteration + for each
set vs array iteration + for each - large arrays
Comments
Confirm delete:
Do you really want to delete benchmark?