let a = Array(600000).fill().map(() => ({a: 1}));
let a = [];
for(let i = 0; i< 600000;i++) a.push({a: 1});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
fill + map | |
push |
Test name | Executions per second |
---|---|
fill + map | 41.0 Ops/sec |
push | 46.3 Ops/sec |
Let's break down the test case and explain what's being tested.
Benchmark Definition
The benchmark definition is a JSON object that describes the test scenario. In this case, we have two test cases:
Array(600000).fill().map(() => ({a: 1}))
. The purpose of this test is to measure how fast it can create and populate an array using the .fill()
method followed by the .map()
method.for
loop. The goal here is to see how fast it can add a large number of elements to an array using the push()
method.Options Compared
In this test case, we're comparing two approaches:
.fill()
followed by .map()
: This approach creates an array with the specified length and then maps over each element, applying a function to create a new object.for
loop with push()
: This approach creates an empty array and then adds 600,000 elements to it using a for
loop.Pros and Cons of Each Approach
.fill()
followed by .map()
:for
loop with push()
: Library Used
In the benchmark definition, there is no explicit library used. However, the .map()
method uses the Array.prototype object, which includes various utility methods and functions provided by JavaScript.
Special JS Features or Syntax
There are no special features or syntax mentioned in this test case.
Now, let's look at the latest benchmark result:
The results show the performance of each test case on a specific browser version (Chrome 104) running on a Mac OS X 10.15.7 device. The ExecutionsPerSecond
value represents how many iterations the test can complete in one second.
Based on these results, we can see that the push()
approach is slightly faster than the .fill()
followed by .map()
approach, with approximately 5-6 executions per second more for the same iteration count. However, please note that this result may vary depending on your specific environment and JavaScript engine.
Other Alternatives
There are other ways to create an array of elements in JavaScript, such as:
Array.from()
with a function or array initializernew Array(600000)
)Keep in mind that these alternatives may have their own trade-offs in terms of performance, readability, and maintainability.