Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set vs array iteration (but better)
(version: 0)
Comparing performance of:
Array iteration vs Set iteration
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const genRandArr = () => Array.from({ length: 1e7 }).map(() => Math.random()); var arr = genRandArr(); var set = new Set(genRandArr());
Tests:
Array iteration
let sum = 0; for (const el of arr) sum += el; console.log('Array result: ', sum);
Set iteration
let sum = 0; for (const el of set) sum += el; console.log('Set result: ', sum);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array iteration
Set iteration
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array iteration
16.1 Ops/sec
Set iteration
19.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand what's being tested in the MeasureThat.net benchmark. **Benchmark Overview** The benchmark measures the performance of two approaches: iterating over an array and an iterable (in this case, a Set object). The goal is to determine which approach is faster for summing up elements in these data structures. **Options Compared** There are two options being compared: 1. **Array Iteration**: This involves using a traditional `for` loop to iterate over the elements of an array. 2. **Set Iteration**: This involves using a `for...of` loop to iterate over the elements of a Set object, which is a data structure that automatically eliminates duplicates. **Pros and Cons** * **Array Iteration**: + Pros: Widely supported, easy to understand, and often the first approach that comes to mind when dealing with arrays. + Cons: Can be slower than Set iteration for large datasets due to overhead from indexing and bounds checking. * **Set Iteration**: + Pros: Can be faster for large datasets because it avoids the overhead of indexing and bounds checking, and it's designed to eliminate duplicates, which can reduce the number of iterations required. + Cons: Less widely supported than array iteration, and may require additional setup (e.g., converting an array to a Set). **Libraries Used** * None explicitly mentioned in the benchmark definition. However, note that the `Set` object is being used here, which is a built-in JavaScript object. **Special JS Features or Syntax** None mentioned in this specific benchmark. **Other Considerations** * **Cache locality**: In array iteration, each element is accessed sequentially, which can lead to good cache locality (i.e., consecutive elements are stored in adjacent memory locations). Set iteration may have poor cache locality due to the random ordering of elements. * **Branch prediction**: Modern CPUs often use branch prediction to improve performance by assuming that certain branches will be taken. In array iteration, the loop condition is checked at each iteration, which can lead to false positives and slow down performance. Set iteration avoids this issue. **Alternative Approaches** Other approaches you might consider for benchmarking set vs array iteration include: * Using a different data structure, such as a `Map` or a linked list. * Adding noise or randomness to the input data to simulate real-world scenarios. * Using parallel processing or multi-threading to compare performance across multiple CPU cores. Keep in mind that these alternatives may not be directly comparable to the original benchmark, and you should carefully consider your specific use case and requirements when selecting alternative approaches.
Related benchmarks:
new Uint8Array() vs Uint8Array.from()
Array push or set
new Array() vs Array.from() with random data
new Uint8Array() vs Uint8Array.from() reverse
Array.from VS spreading for
Comments
Confirm delete:
Do you really want to delete benchmark?