Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array Initialization Types
(version: 0)
Comparing performance of:
Array Constructor vs Array Size Constructor vs Array Literal
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var times = 5000;
Tests:
Array Constructor
var a = new Array(); for (var i = 0; i <= times; i++) { a.push(1337) }
Array Size Constructor
var b = new Array(times); for (var i = 0; i <= times; i++) { b.push(1337) }
Array Literal
var c = []; for (var i = 0; i <= times; i++) { c.push(1337) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array Constructor
Array Size Constructor
Array Literal
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array Constructor
1233.8 Ops/sec
Array Size Constructor
1350.1 Ops/sec
Array Literal
1797.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark measures the performance of three different ways to initialize an array in JavaScript: using the `new Array()` constructor, `new Array(times)` constructor, and an array literal (`[]`). **Test Cases** There are three test cases: 1. **Array Constructor**: This test case uses the `new Array()` constructor to create an array and then pushes 1337 into it `times` number of times. 2. **Array Size Constructor**: This test case uses the `new Array(times)` constructor to create an array with a specific size (`times`) and then pushes 1337 into it `times` number of times. 3. **Array Literal**: This test case uses an array literal (`[]`) to create an empty array and then pushes 1337 into it `times` number of times. **Library Usage** None of the test cases use any external libraries. **Special JavaScript Features/Syntax** None of the test cases utilize any special JavaScript features or syntax. They only rely on standard JavaScript features and syntax. **Benchmark Comparison** The benchmark compares the performance of these three methods: * **Array Constructor**: Creates an array using `new Array()` and then pushes elements into it. + Pros: Easy to read and maintain, concise code. + Cons: Can lead to slower performance due to the creation overhead of the new array object. * **Array Size Constructor**: Creates an array using `new Array(times)` and then pushes elements into it. + Pros: More efficient than `new Array()` since it creates an array with a specific size, reducing the overhead. + Cons: Requires knowing the exact size of the array beforehand, which might not be always possible or desirable. * **Array Literal**: Creates an empty array using `[]` and then pushes elements into it. + Pros: Very efficient since arrays created using literals are optimized for performance. + Cons: May lead to confusing code if the literal is too long or complex. **Benchmark Results** The latest benchmark results show that: * The **Array Literal** method performs best, with an average of 1796.97998046875 executions per second on a Desktop (Mac OS X 10.15.7) running Chrome 119. * The **Array Size Constructor** method performs worst, with an average of 1233.767578125 executions per second on the same platform and browser. These results suggest that using an array literal is generally the most efficient way to initialize an array in JavaScript. **Other Alternatives** If you need to create a large array or an array with a specific size, you might consider using other alternatives: * **Array.from()**: Creates an array from an iterable source. While it's not directly comparable to the above methods, it can be a more efficient way to create arrays in certain scenarios. * **Sparse Arrays**: Create a sparse array by using `new Array(times)` with a specific length and then filling some elements with values. * **Typed Arrays**: Use typed arrays (e.g., Int32Array) for numerical computations or other types of data that require efficient memory access. Keep in mind that these alternatives might not be as straightforward to use as the original methods, but they can provide additional options depending on your specific use case.
Related benchmarks:
Preinitialized array size vs Push operations to an empty one.
Spread or Push
empty an array in JavaScript?(Yorkie)1
for vs fill
Hardcoded Array vs Array.from() vs new Array() vs push
Comments
Confirm delete:
Do you really want to delete benchmark?