var n = 100000;
var arr = new Array(n);
for (var i = 0; i < n; i++) {
arr[i] = i;
}
var n = 100000;
var arr = [];
for (var i = 0; i < n; i++) {
arr.push(i);
}
var n = 100000;
var arr = [];
for (var i = 0; i < n; i++) {
arr[i] = i;
}
var n = 100000;
var arr = [];
arr.length = n;
for (var i = 0; i < n; i++) {
arr[i] = i;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array constructor - 100000 items | |
Array literal - 100000 items | |
Array literal (assign by index) - 100000 items | |
Array literal (explicit length) - 100000 items |
Test name | Executions per second |
---|---|
Array constructor - 100000 items | 1394.6 Ops/sec |
Array literal - 100000 items | 583.6 Ops/sec |
Array literal (assign by index) - 100000 items | 428.9 Ops/sec |
Array literal (explicit length) - 100000 items | 1457.2 Ops/sec |
Measuring the performance of different approaches to creating arrays in JavaScript is crucial, as it can impact the efficiency and scalability of many applications.
The provided benchmark test measures the time taken by four different methods to create an array of 100,000 elements:
new Array()
syntax.var arr = new Array(n);
Pros: Simple and straightforward syntax. Cons: Can be slower due to the overhead of creating a new object.
[]
and assigns values to each element using the indexing syntax (arr[i] = i;
).var arr = [];
for (var i = 0; i < n; i++) {
arr.push(i);
}
Pros: Can be faster since it avoids the overhead of creating a new object.
Cons: Requires an extra push()
call to add elements, which can lead to additional memory allocations.
arr.length = n;
and then assigns values to each element using indexing syntax (arr[i] = i;
).var arr = [];
arr.length = n;
for (var i = 0; i < n; i++) {
arr[i] = i;
}
Pros: Similar to assign by index, but avoids the extra push()
call.
Cons: Requires an extra property access (arr.length
).
var arr = new Array(n); var len = n;
) to avoid issues with object length.var arr = new Array(n);
var len = n;
for (var i = 0; i < len; i++) {
arr[i] = i;
}
Pros: Similar to the original array constructor method, but avoids potential issues with object length.
Cons: Additional variable len
is required.
The provided benchmark results show that:
Other alternatives for creating arrays in JavaScript include using the spread operator ([...new Array(n)]
) or Array.from()
methods. However, these approaches may not be supported in older browsers and may have performance implications of their own.
Here is an example of how you can create an array using the spread operator:
var arr = [...new Array(n)];
And here's an example using Array.from()
:
var arr = Array.from(new Array(n));
Keep in mind that these approaches may not be as efficient as the methods tested in this benchmark, and their performance may vary depending on the specific use case.
In summary, when it comes to creating arrays in JavaScript, the order of operations matters. The most important thing is to choose a method that balances readability with performance.