const array = window.array = []
for (let i = 1000; i; --i) array.push(i)
window.a = Array.from(array)
window.b = [array]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.from | |
Spread |
Test name | Executions per second |
---|---|
Array.from | 1239716.8 Ops/sec |
Spread | 1404474.2 Ops/sec |
I'll break down the explanation into smaller sections to make it easier to understand.
Benchmark Definition and Purpose
The provided JSON represents a JavaScript microbenchmark named "Cloning array: Array.from vs spread corrected". The benchmark aims to compare the performance of two approaches for cloning an array:
Array.from(array)
[...array]
(also known as the spread operator)The purpose of this benchmark is to determine which method is faster and more efficient for creating a new copy of an array.
Options Compared
The two options being compared are:
Array.from(array)
: This method creates a new array by iterating over the elements of the original array using the from()
method.[...array]
(spread operator): This method uses the spread operator to create a new array by copying the elements of the original array.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
Array.from(array)
.Map
and Set
[...array]
(spread operator).Library Usage
There is no specific library used in this benchmark. However, the Array.from()
method was introduced in ECMAScript 2015 (ES6) and became widely adopted.
Special JS Features or Syntax
This benchmark does not use any special JavaScript features or syntax beyond what's required for array cloning.
Other Alternatives
If you're interested in exploring other approaches for array cloning, here are a few alternatives:
slice()
: This method creates a shallow copy of an array by returning a new array object with references to the same elements.concat()
: This method creates a new array by concatenating two or more arrays together.clone()
function, which can create a deep copy of an array.Keep in mind that these alternatives may have different performance characteristics and use cases compared to the Array.from()
and spread operator methods being benchmarked here.
I hope this explanation helps you understand the test case!