Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set vs array add 1k items
(version: 0)
Comparing performance of:
array vs set
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
window.__arr = [] window.__set = new Set()
Tests:
array
const arr = window.__arr for (let i = 0; i < 1_000; i++) { arr.push({}) }
set
const set = window.__set for (let i = 0; i < 1_000; i++) { set.add({}) }
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:
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 break down the provided benchmark and explain what's being tested, compared, and some pros and cons of each approach. **Benchmark Overview** The benchmark measures the performance difference between using an array and a Set data structure in JavaScript to add 1,000 items. The test case consists of two benchmarks: "array" and "set". **Script Preparation Code** Before running the benchmarks, the script preparation code initializes two variables: * `window.__arr`: an empty array * `window.__set`: a new Set object This code is executed only once before each benchmark iteration. **Benchmark Definitions** Each test case has its own benchmark definition in JSON format. These definitions specify how to generate data for the benchmark: 1. **Array Benchmark**: The script defines a constant `arr` and populates it with 1,000 empty objects using a for loop: ```javascript const arr = window.__arr; for (let i = 0; i < 1_000; i++) { arr.push({}); } ``` 2. **Set Benchmark**: The script defines a constant `set` and populates it with 1,000 empty objects using a for loop: ```javascript const set = window.__set; for (let i = 0; i < 1_000; i++) { set.add({}); } ``` **Comparison** The benchmark compares the performance of adding 1,000 items to an array versus a Set. The results are stored in two separate arrays: * `rawUAString`: represents the user agent string of the browser running the test * `browser`, `devicePlatform`, and `operatingSystem` provide information about the testing environment * `executionsPerSecond` measures the number of operations executed per second for each benchmark **Pros and Cons** * **Array**: Pros: + Easy to implement and understand + Can be optimized with techniques like sparse arrays or using a fixed-size array * Cons: + May suffer from performance issues due to slow indexing operations (e.g., `arr[i]`) * Set: Pros: + Efficient for adding new elements, as it doesn't require searching for an empty slot + Fast lookup and insertion times Cons: * More complex implementation due to the need for a hash table * May have higher overhead compared to arrays **Library and Special Features** In this benchmark, the Set data structure is used from the built-in JavaScript `Set` object. There are no special JS features or syntax used in this benchmark. **Alternatives** Other alternatives to using an array and a Set could include: * Using other data structures like `WeakMap`, `WeakSet`, or custom implementations * Optimizing array or Set operations with techniques like caching, memoization, or parallel processing * Using a different programming language or paradigm (e.g., functional programming) Overall, this benchmark provides a good starting point for understanding the performance differences between arrays and Sets in JavaScript.
Related benchmarks:
Create Set vs loop
set vs some
set vs some 1000000
set has vs array includes (1k items)
Comments
Confirm delete:
Do you really want to delete benchmark?