Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array initialization: preallocate vs push with objects
(version: 1)
Comparing performance of:
Preallocate vs Push
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
Preallocate
var N = 10000; var result = new Array(N); for (var i = 0; i < N; i++) { result[i] = {n: N}; }
Push
var N = 10000; var result = []; for (var i = 0; i < N; i++) { result.push({n: 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:
29 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Preallocate
24634.4 Ops/sec
Push
17977.8 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark you provided evaluates two different methods of initializing and populating an array in JavaScript: "Preallocate" and "Push". ### Approach Comparison 1. **Preallocate Method** - **Code**: ```javascript var N = 10000; var result = new Array(N); for (var i = 0; i < N; i++) { result[i] = {n: N}; } ``` - **Description**: This method preallocates memory for an array with a set size (N). The array is created with `new Array(N)`, which reserves space for 10,000 elements upfront. Then, the loop fills the array with objects. - **Pros**: - **Memory Allocation**: Allocating memory in one step can be more efficient because it minimizes the number of memory re-allocations needed when the array grows. - **Initial Setup**: Faster when the number of items being added is known beforehand as it doesn't involve the overhead of dynamic resizing. - **Cons**: - If more elements are added beyond the preallocated size, it requires reallocation. - Initializing an unused array could consume memory unnecessarily if the pre-allocation isn't fully utilized. 2. **Push Method** - **Code**: ```javascript var N = 10000; var result = []; for (var i = 0; i < N; i++) { result.push({n: N}); } ``` - **Description**: This method starts with an empty array and uses the `push()` method to add 10,000 objects to it during the loop. - **Pros**: - **Dynamic Sizing**: It allows for a dynamically sized array which can accommodate an uncertain number of elements. - **Simplicity**: It is easy to understand and straightforward, particularly when the final size of the array is not known. - **Cons**: - Memory reallocation might occur as the array grows, which can lead to performance penalties, especially if the elements exceed the initially allocated space. ### Benchmark Results From the provided benchmark results: - **Preallocate** achieved **15,464.01 executions per second**. - **Push** achieved **14,628.36 executions per second**. ### Considerations The benchmark indicates that the "Preallocate" method outperformed the "Push" method in this specific test case. However, the actual performance can vary depending on factors like the JavaScript engine optimization, the execution environment, and how the benchmarks are run (e.g., browser version, or device used). ### Alternative Techniques Other alternatives to consider for array initialization and population include: - **Using Typed Arrays**: For performance-critical applications, such as numerical computations, Typed Arrays provide an alternative that utilizes a contiguous block of memory and can offer better performance with numerical data. Examples include `Int32Array`, `Float64Array`, etc. - **Using Array.from**: This method allows for array creation based on array-like structures or iterable objects. This can be useful for creating arrays from existing collections efficiently. - **Spread Operator**: The spread operator (`...`) can be used in newer JavaScript to expand iterable structures into array literals, which might be more readable in some scenarios. In summary, choosing between these approaches depends on the specific requirements of your application, such as performance needs, memory considerations, and coding style preferences.
Related benchmarks:
Array constructor vs literal
Array constructor vs literal
Array constructor vs literal performance - 1000 items
Array initialization: preallocate vs push
Array initialization: preallocate vs push vs preallocate & fill
Array initialization: preallocate_add_end vs push
Set->Array.from vs iterative pushing
Set->Array.from vs iterative pushing 10k
Array initialization: preallocate vs push (Objects)
Comments
Confirm delete:
Do you really want to delete benchmark?