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.slice(0)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
map | |
array.from | |
for loop | |
foreach | |
slice | |
slice(0) |
Test name | Executions per second |
---|---|
map | 82222264.0 Ops/sec |
array.from | 14852244.0 Ops/sec |
for loop | 67716352.0 Ops/sec |
foreach | 71022856.0 Ops/sec |
slice | 176373568.0 Ops/sec |
slice(0) | 184637504.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark with six individual test cases, each testing a different approach for copying an array.
Benchmark Definition
The benchmark definition is a JSON object that describes the experiment:
Name
: A unique name for the benchmark.Description
: An optional description of the benchmark (none in this case).Script Preparation Code
and Html Preparation Code
: These fields are empty, suggesting that no specific code needs to be prepared or rendered before running the benchmark.Test Cases
Each test case is represented by a JSON object with the following properties:
Benchmark Definition
: A string containing the JavaScript code for the test case.Test Name
: The name of the test case.Let's analyze each test case:
map()
method, which creates a new array with the results of applying a provided function to each element in the original array.Array.from()
method, which creates a new array from an iterable (such as another array or an array-like object).for
loop to iterate over the elements of an array and copy them to a new array.forEach()
method, which executes a provided function for each element in an array without returning any value.slice()
method, which returns a shallow copy of a portion of an array.slice()
.Libraries and Special Features
Array.from()
uses the Iterable
API, which is a modern feature introduced in ECMAScript 2015 (ES6). This allows for more flexible and efficient iteration over various types of iterables.Array.from()
relies on the Iterable
API.Other Alternatives
In addition to the methods tested by this benchmark, other approaches for copying arrays include:
reduce()
: While not shown in this benchmark, reduce()
can be used to create a new array by iterating over the elements of an original array.concat()
: This method returns a new array containing all elements from both arrays; it may incur significant memory overhead if used with large arrays.Splice()
: This method modifies the original array and returns a modified array object; it may not be suitable for creating a new, independent copy of an array.Keep in mind that this benchmark is focused on measuring the performance of specific methods for copying arrays. Other approaches may have different trade-offs regarding memory usage, readability, and maintainability.