const arr = [1,2,3,4,5]
const newArr = arr.map(e=>e)
const arr = [1,2,3,4,5]
const newArr = Array.from(arr)
const arr = [1,2,3,4,5]
let newArr = [];
for(let i = 0; i < arr.length; i++) {
newArr[i] = arr[i];
}
const arr = [1,2,3,4,5]
let newArr = []
arr.forEach(e => {
newArr.push(e)
})
const arr = [1,2,3,4,5]
const newArr = arr.slice()
const arr = [1,2,3,4,5]
const newArr = [arr]
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
map | |
array.from | |
for loop | |
foreach | |
slice | |
spread |
Test name | Executions per second |
---|---|
map | 10653647.0 Ops/sec |
array.from | 4118789.2 Ops/sec |
for loop | 15137293.0 Ops/sec |
foreach | 13655042.0 Ops/sec |
slice | 13536197.0 Ops/sec |
spread | 7216431.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The test compares the performance of six different ways to create a new array from an existing one in JavaScript:
map()
Array.from()
forEach()
slice()
...
)Options Compared
Each option is compared in terms of its execution speed, with the goal of determining which method is the fastest.
Pros and Cons of Each Approach
...
):Library and Its Purpose
None of the libraries are explicitly mentioned in the provided information. However, Array.from()
is a built-in method that was introduced in ECMAScript 2015 (ES6).
Special JS Features or Syntax
The spread operator (...
) is used to create a new array from an existing one.
Benchmark Results
The latest benchmark results show the execution speed of each option for the given test case. The browser and device platform are Opera 98 on Windows, with an average execution speed of:
map()
: 80979136 executions per secondArray.from()
: 8189305 executions per second...
): 43579192 executions per secondforEach()
: 42868856 executions per secondslice()
: 112151200 executions per secondOther Alternatives
Some alternative methods for creating a new array from an existing one include:
concat()
: creates a new array by concatenating two or more arrays.reduce()
: can be used to create a new array, although it's not as concise or efficient as the other options.Keep in mind that these alternatives may have different performance characteristics and use cases compared to the methods being tested in this benchmark.