[1,2,3,5,7,7,8,9,10,11,12,14,14,16,17,17,17,17,18,19,22,22,23,23,25,26,27,27,28,32,34,35,36,36,39,39,39,41,42,43,44,44,44,47,49,49,50,50,51,53,53,56,57,57,58,58,59,60,60,61,62,62,62,63,63,63,65,65,66,67,68,68,69,73,74,75,75,76,77,78,79,79,83,85,85,86,87,87,88,89,91,92,92,93,95,95,98,98,100].map(i => console.log(i))
Array.from(Array(100)).map(i => console.log(i))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Simple Array | |
With Array from |
Test name | Executions per second |
---|---|
Simple Array | 13240.2 Ops/sec |
With Array from | 12029.9 Ops/sec |
Let's break down the provided JSON benchmark and its test cases to understand what is being tested.
What is being tested?
The benchmark is comparing two approaches for creating an array in JavaScript: using a traditional Array
constructor versus using the Array.from()
method. The goal of this benchmark is to determine which approach is faster.
Options compared:
Array
constructor, where you pass an initial value (in this case, an empty array) and then use the map()
function to iterate over it.Array.from()
method, which creates a new array by copying elements from an iterable (such as another array or string).Pros and Cons of each approach:
Array.from()
is large).Library/Functionality used:
None explicitly mentioned in this benchmark. However, map()
and the Array
constructor are built-in JavaScript functions.
Special JS feature or syntax:
The test cases use map()
which is a common JavaScript function that applies a provided function to each element of an array.
Benchmark preparation code and HTML preparation code:
Since there is no specific code required for benchmark preparation, it's assumed that the provided script(s) are already executed before running the benchmark. The same goes for HTML preparation code; it's likely that some basic HTML structure (e.g., a container element) is set up to execute the scripts.
Other alternatives:
Some alternative approaches could be:
Array.prototype.slice()
or Array.prototype.splice()
instead of map()
.Keep in mind that these alternatives might not be directly comparable to the original Simple Array and With Array from approaches, as they might introduce additional overhead or complexity.