var arraySize = 10000;
var iterations = arraySize + 100;
var data;
var operate = function(){
for(var index = 0; index < iterations; index++){
data.unshift({});
if(data.length > arraySize) data.pop();
}
}
data = new Array(arraySize);
operate();
data = [];
operate();
data = [];
data.length=arraySize;
operate();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
new Array | |
brackets | |
brackets with length |
Test name | Executions per second |
---|---|
new Array | 32.5 Ops/sec |
brackets | 56.9 Ops/sec |
brackets with length | 23.6 Ops/sec |
Let's dive into the explanation.
Benchmark Definition JSON Analysis
The provided benchmark definition represents a test for populating an array in JavaScript. Here, we have three variations:
new
keyword and initializes it with zeros.[]
to create an empty array.The script preparation code defines a function operate()
that populates the array by unshifting objects onto it, removing elements if the length exceeds the initial array size (arraySize
). The goal is to measure how these different approaches impact performance.
Options Compared
We have two primary options being compared:
new
keyword to create an array.[]
to create an empty array.Each option has its pros and cons:
new
keyword involves additional function calls.new
keyword.Library Used (None)
In this benchmark, no specific JavaScript library is used. The script preparation code and individual test cases rely solely on built-in JavaScript functionality.
Special JS Feature/Syntax (None)
There are no special JavaScript features or syntax being tested in this benchmark.
Benchmark Result Analysis
The latest benchmark result shows the executions per second for each test case across a few different browsers:
These results suggest that, in this specific scenario, using brackets []
for array creation yields the best performance.
Other Alternatives
If you're interested in exploring alternative approaches, here are a few options:
Array()
, ArrayConstructor
, or Array.prototype
methods instead of new Array
.Keep in mind that these alternatives may introduce additional complexity, performance overhead, or errors, so it's essential to carefully evaluate their suitability for your specific use case.