Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array initialization: preallocate vs push vs preallocate & fill
(version: 1)
Comparing performance of:
Preallocate vs Preallocate & fill vs Push vs Index
Created:
3 years ago
by:
Registered User
Jump to the latest result
Tests:
Preallocate
var N = 10000; var result = new Array(N); for (var i = 0; i < N; i++) { result[i] = i * 2; }
Preallocate & fill
var N = 10000; var result = new Array(N).fill(0); for (var i = 0; i < N; i++) { result[i] = i * 2; }
Push
var N = 10000; var result = []; for (var i = 0; i < N; i++) { result.push(i * 2); }
Index
var N = 10000; var result = []; for (var i = 0; i < N; i++) { result[i] = i * 2; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Preallocate
Preallocate & fill
Push
Index
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Preallocate
18162.3 Ops/sec
Preallocate & fill
11908.4 Ops/sec
Push
17277.1 Ops/sec
Index
15067.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **Benchmark Definition** The benchmark is defined in JSON format, which represents the test cases to be executed. The test case measures the performance of different approaches for initializing an array: `preallocate`, `push`, and `index`. Each test case has a unique name, description (none in this case), preparation code, and HTML preparation code. **Approaches Compared** The three approaches compared are: 1. **Preallocate**: Allocates memory for the entire array beforehand using `new Array(N)`. 2. **Preallocate & Fill**: Initializes the array with zeros using `new Array(N).fill(0)` before populating it. 3. **Push**: Uses the `push` method to add elements one by one. **Pros and Cons of Each Approach** 1. **Preallocate**: * Pros: Typically faster, as the entire array is allocated upfront. * Cons: Requires more memory upfront, which can lead to slower performance if the array grows larger than expected. 2. **Preallocate & Fill**: * Pros: Reuses the same allocation for all elements, reducing overhead. * Cons: Slower than preallocation since it involves a separate step of filling the array with zeros. 3. **Push**: * Pros: Memory-efficient, as only necessary space is allocated for each element. * Cons: Typically slower due to repeated allocations and pushes. **Library Used** There doesn't appear to be any specific library used in this benchmark. The JavaScript code uses built-in methods like `Array`, `new Array()`, `push()`, and `fill()`. **Special JS Feature/Syntax (None)** No special JavaScript features or syntax are used in this benchmark. **Other Considerations** * **Cache Locality**: Preallocation can improve cache locality, as the allocated array is contiguous in memory. This might lead to better performance for certain workloads. * **Garbage Collection**: The `push` approach can incur more garbage collection overhead due to the repeated allocations and deallocations of individual elements. **Alternative Approaches** Other approaches that could be explored in this benchmark include: 1. **Array.from()**: Using the `Array.from()` method to create an array from an iterable. 2. **map()**: Using the `map()` method to create a new array by applying a transformation function to each element of an existing array. 3. **WebAssembly**: Compiling JavaScript code to WebAssembly (WASM) for potential performance benefits. Please note that these alternatives might not be directly comparable to the preallocated, push, and indexed approaches, as they involve different algorithmic designs and execution patterns.
Related benchmarks:
Array construct vs array push
Array fill method vs push in for loop
Array.from() vs new Array() vs push
Spread vs Push when adding into array
Array.from() vs new Array() vs push pushup
Comments
Confirm delete:
Do you really want to delete benchmark?