var n = 10;
var arr = new Array(n);
for (var i = 0; i < n; i++) {
arr[i] = i;
}
var n = 10;
var arr = [];
for (var i = 0; i < n; i++) {
arr.push(i);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array constructor - 10 items | |
Array literal - 10 items |
Test name | Executions per second |
---|---|
Array constructor - 10 items | 4016722.5 Ops/sec |
Array literal - 10 items | 17021012.0 Ops/sec |
Measuring the performance of JavaScript array creation using either the new Array
constructor or an array literal is a fundamental benchmarking exercise.
What's being tested?
In this case, two different approaches are compared:
Array
constructor with an integer argument, which specifies the initial length of the array.[]
and then pushing elements onto it using the push()
method.Options compared:
The two approaches have different characteristics that affect their performance:
Pros and Cons:
Array constructor:
Pros:
Cons:
Array literal:
Pros:
Cons:
Library usage:
In this benchmark, neither library is explicitly mentioned. However, some JavaScript engines may use libraries like V8 (used by Google Chrome) or SpiderMonkey (used by Mozilla Firefox) that provide optimized implementations for common tasks, including array creation.
Special JS feature/syntax:
None are mentioned in the provided benchmark definitions.