Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set vs array creation
(version: 0)
Comparing performance of:
array vs set
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
array
const MAX = 10000 const arr = new Array(10000) const f = () => {} for (let i = 0; i < MAX;i++) { arr[i] = true if (arr[i]) f(); }
set
const set = new Set() const MAX = 10000 const f = () => {} for (let i = 0; i < MAX;i++) { set.add(i) if (set.has(i)) f(); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array
set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 YaBrowser/24.7.0.0 Safari/537.36
Browser/OS:
Yandex Browser 24 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array
115781.7 Ops/sec
set
3077.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The benchmark in question compares two approaches: creating an array versus creating a set, both for setting 10,000 elements. **Approaches Compared** There are two test cases: 1. **Array Creation**: This approach creates an empty array of size 10,000 using the `new Array(10000)` constructor. The code then populates the array with values and uses the `arr[i]` syntax to access each element. 2. **Set Creation**: This approach creates an empty set using the `new Set()` constructor. The code then adds 10,000 elements to the set using the `set.add(i)` method. **Pros and Cons of Each Approach** ### Array Creation Pros: * More efficient for numerical data, as arrays store values in contiguous memory locations. * May be faster for large datasets due to less overhead from managing internal node structures. Cons: * Less flexible than sets, which can only store unique values (e.g., no duplicates). * Requires manual indexing and bounds checking using `arr[i]`. ### Set Creation Pros: * More convenient and expressive syntax for adding elements (`set.add(i)`), as it automatically eliminates duplicates. * Easier to check membership in a set using the `set.has(i)` method. Cons: * Less efficient than arrays, especially for numerical data, due to the overhead of managing internal node structures. * May be slower for large datasets due to additional memory allocation and garbage collection. **Library Used** Neither approach relies on an external library. However, if we consider the inherent properties of JavaScript objects (arrays and sets), we can note that: * Arrays are a built-in JavaScript object, whereas sets are part of the ECMAScript standard. * The `new Set()` constructor is only available in ECMAScript 2015+ dialects. **Special JS Features/Syntax** There are no specific special features or syntax mentioned in the benchmark. However, it's worth noting that the use of arrow functions (`() => {}`) and the `for...of` loop (not used explicitly but implied by the context) is a relatively modern JavaScript feature introduced in ECMAScript 2015. **Other Alternatives** For larger-scale benchmarking or more complex scenarios, consider exploring other alternatives: * **Benchmarking frameworks**: e.g., Benchmark.js, test.js, or JSPerf. * **Profiling tools**: e.g., Chrome DevTools, Node.js Inspector, or Firefox Developer Edition's Performance Profiler. Keep in mind that each framework or tool has its strengths and weaknesses. MeasureThat.net provides a simple, lightweight way to create and run microbenchmarks, which is particularly suitable for small-scale comparisons like this one.
Related benchmarks:
set vs array includes
Array creation vs Set creation
fromArray or desctucturing to convert Set to array
Create Set vs loop
set vs array iteration new new
Comments
Confirm delete:
Do you really want to delete benchmark?