<!--your preparation HTML code goes here-->
/*your preparation JavaScript code goes here
To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/
async function globalMeasureThatScriptPrepareFunction() {
// This function is optional, feel free to remove it.
// await someThing();
}
var arr = ['', '', ''];
const ARRAY_LENGTH = 10000;
new Array(ARRAY_LENGTH).fill(0).map((_, index) => index)
Array.from({ length: ARRAY_LENGTH }, (_, index) => index)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
new Array + fill | |
Array.from |
Test name | Executions per second |
---|---|
new Array + fill | 37061.2 Ops/sec |
Array.from | 67548.2 Ops/sec |
The benchmark defined in the provided JSON measures and compares the performance of two different methods to create an array of a specified length, filling it with values that represent their indices. Specifically, it evaluates the following approaches:
new Array(ARRAY_LENGTH).fill(0).map((_, index) => index)
- This creates an array with a given length, fills it with zeros, and then maps each index to its corresponding value.Array.from({ length: ARRAY_LENGTH }, (_, index) => index)
- This uses the Array.from
method, which creates a new array from an array-like or iterable object. Here, it specifically builds an array of the specified length and uses the mapping function to set each index to its corresponding value.new Array(length).fill(0).map(...)
:Pros:
fill(0)
initializes the array before mapping, making it explicit that the intent is to populate it.Cons:
Array.from(...)
:Pros:
Array.from
is often more efficient because it directly creates an array with the desired values, eliminating the need for an intermediate filled array.Cons:
Array.from
works, especially if they are not accustomed to ES6+ features.According to the benchmark results:
new Array + fill
achieved approximately 31,618.73 executions per second.Array.from
managed about 4,176.43 executions per second.These results highlight that, despite the Array.from
method being declared as a more efficient way to create and map arrays, in this specific benchmark, the new Array + fill
approach performed significantly better. This outcome may be influenced by various factors, including the specific environment in which the benchmark was run (e.g., pattern optimizations in the JavaScript engine).
for
loop: Developers might also consider using a traditional loop (e.g., a for
or forEach
loop) to create and fill an array. This approach can be very flexible and may provide performance benefits in certain scenarios because it avoids the overhead of additional array methods.
Spread syntax: Another alternative could be using the spread operator in combination with Array.keys()
, such as [...Array(ARRAY_LENGTH).keys()]
, to create an array of indices. This is easy to read but may not have as optimal performance.
Direct assignment: In scenarios where the array length is known, directly assigning values using a loop (incrementing index) could be the most performant way, especially for larger datasets.
In summary, while both methods offer valid ways to generate an array of indices, performance can vary significantly based on context, and developers should consider profiling both options in their specific applications. Understanding the underlying principles of array manipulation in JavaScript is critical for optimizing performance effectively.