Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array access vs set access
(version: 0)
Comparing performance of:
Array access vs Set access
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arrData = []; var setData = new Set(); var iter = 100; for (i = 0; i < iter; i++) { arrData.push(i); setData.add(i); }
Tests:
Array access
for (i = 0; i < (iter * 2); i++) { if (i in arrData) {} }
Set access
for (i = 0; i < (iter * 2); i++) { if (setData.has(i)) {} }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array access
Set access
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array access
59025.8 Ops/sec
Set access
60147.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Definition** The benchmark is defined in JSON format, which describes two test cases: 1. "Array access" 2. "Set access" The script preparation code creates an empty array `arrData` and a new Set `setData`. The loop iterates 100 times, adding each number to both the array and the set. **Options Compared** The benchmark is testing two options: * Array access (`if (i in arrData)`) * Set access (`if (setData.has(i))`) These options are compared by running each test case multiple times and measuring the execution time. **Pros and Cons of Each Approach** 1. **Array Access** * Pros: + Faster lookups for small arrays (due to O(1) average case performance) + Can be useful when working with existing data structures * Cons: + Slower for large datasets due to O(n) worst-case performance 2. **Set Access** * Pros: + Faster lookups for large datasets due to O(1) average case performance + Suitable for use cases where uniqueness is important (e.g., removing duplicates) * Cons: + Slower for small datasets due to overhead of creating and managing the set **Library Used** The benchmark uses the `Set` data structure, which is a built-in JavaScript collection type. The Set data structure provides an efficient way to store unique values and perform lookups. **Special JS Feature/Syntax** None mentioned in the provided benchmark definition. **Other Alternatives** If you need to compare array access or set access performance for different data structures, you might consider using: * `Map` instead of `Set` for lookup-heavy use cases * `Linked hash table` (e.g., `WeakMap`) for fast lookups with minimal overhead * Custom data structures (e.g., binary search trees) for optimized performance Keep in mind that the choice of data structure depends on the specific requirements and constraints of your application. **Benchmark Preparation Code** The provided script preparation code is simple and focused on creating an empty array and set, which are then used as test cases. You can modify or extend this code to include additional variables, loops, or operations to better represent real-world scenarios. **Individual Test Cases** Each test case iterates over the range of numbers generated by `iter * 2` times, performing either an array access (`if (i in arrData)`) or a set access (`if (setData.has(i))`). The benchmark measures the execution time for each test case.
Related benchmarks:
recreate array vs set 2
set vs array includes
Array vs Set loopup in Javascript
set.add vs array.push Fabien2
Array push or set
Comments
Confirm delete:
Do you really want to delete benchmark?