Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array initialization: preallocate vs push (Objects)
(version: 0)
Comparing performance of:
Preallocate vs Push
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Preallocate
var N = 1000000; var result = new Array(N); for (var i = 0; i < N; i++) { result[i] = { value: N }; }
Push
var N = 1000000; var result = []; for (var i = 0; i < N; i++) { result.push({ value: N }); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Preallocate
Push
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0
Browser/OS:
Chrome 138 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Preallocate
51.7 Ops/sec
Push
134.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** The provided JSON represents a JavaScript microbenchmarking test case on MeasureThat.net, which compares the performance of two approaches for initializing arrays: preallocating memory and using the `push` method. **Benchmark Definition** The benchmark definition is a simple script that creates an array with a fixed size (1 million elements) and populates each element with a specific object. The script has two variations: 1. **Preallocate**: This approach preallocates memory for the entire array using the `new Array(N)` constructor. 2. **Push**: This approach uses the `push` method to add elements to an empty array. **Options Compared** The benchmark compares the performance of these two approaches on different browsers and devices: * **Browser**: Firefox 127 (a specific version of Mozilla Firefox) * **Device Platform**: Desktop (running on a Linux operating system) * **Operating System**: Linux **Pros and Cons of Each Approach** 1. **Preallocate**: * Pros: Memory allocation is done upfront, which can reduce the overhead of dynamic memory management. * Cons: If the array size exceeds the available memory, it can lead to an error or a performance issue. 2. **Push**: * Pros: Dynamic memory allocation is handled by the JavaScript engine, reducing memory-related issues. * Cons: The `push` method has overhead due to dynamic memory allocation and copying. **Library Usage** None of the provided benchmark cases use any external libraries. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark. The code is straightforward and uses standard JavaScript constructs. **Alternatives** Other approaches for initializing arrays could be considered, such as: * Using a typed array (e.g., `Int32Array`) with preallocation. * Utilizing WebAssembly (WASM) for optimized array initialization. * Implementing a custom array allocation algorithm using low-level memory management libraries like Web Workers. However, these alternatives may not be relevant to the primary goal of this benchmark, which focuses on comparing the performance of two common JavaScript approaches: preallocating memory and using the `push` method.
Related benchmarks:
Spread operator vs array push
Array construct vs array push
Pushing items via Array.push vs. Spread Operator
Array Push vs. Index Access
Spread vs Push when adding into array
Comments
Confirm delete:
Do you really want to delete benchmark?