Array.from({ length: 100 }, (_, i) => i+i);
Array.from({ length: 100 }).map((_, i) => i+i);
Array(100).fill("").map((_, i) => i+i);
Array(100).fill(null).map((_, i) => i+i);
Array(100).fill(null).map((_, i) => i+i);
Array(100).fill(true).map((_, i) => i+i);
Array(100).fill(0).map((_, i) => i+i);
const a = new Array(100)
for(let i = 0; i < a.lenght; i++)
a[i] = i+i
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.from() only | |
Array.from().map() | |
.fill("").map() | |
.fill(null).map() | |
.fill(undefined).map() | |
.fill(true).map() | |
.fill(0).map() | |
For loop |
Test name | Executions per second |
---|---|
Array.from() only | 120050.1 Ops/sec |
Array.from().map() | 126393.0 Ops/sec |
.fill("").map() | 1114921.9 Ops/sec |
.fill(null).map() | 1074070.9 Ops/sec |
.fill(undefined).map() | 1114301.0 Ops/sec |
.fill(true).map() | 1127919.9 Ops/sec |
.fill(0).map() | 1174946.9 Ops/sec |
For loop | 3496883.0 Ops/sec |
Let's break down the benchmark and its test cases.
Benchmark Definition
The benchmark is called "fill then map (with null/undefined/""/0) vs for loop vs array.from". This name suggests that the benchmark is comparing three different approaches to achieve a similar result:
fill
method followed by map
methodArray.from()
constructorOptions Compared
The benchmark compares these three options in terms of their performance, which can be measured by the number of executions per second.
Pros and Cons of Each Approach
Array.from()
can provide better performance than manual indexing and assignment, as it leverages the browser's optimized array construction algorithm. However, it may not be as efficient as the fill
method followed by map
, especially for large arrays.Library Used
In this benchmark, no specific library is used beyond the built-in JavaScript functions like Array.from()
, map()
, and fill()
.
Special JS Features or Syntax
There are no special JS features or syntax used in these test cases. They use standard JavaScript constructs to demonstrate the performance differences between the three approaches.
Other Alternatives
If you're interested in exploring other alternatives, here are a few options:
map()
and then concatenating/merging arrays, you can use reduce()
to accumulate results.In conclusion, this benchmark provides a straightforward way to compare the performance of different approaches for common array operations. By understanding the trade-offs and considerations mentioned above, you can choose the best approach depending on your specific use case and requirements.