var data = [];
var res = [];
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(json => data = json)
res = data.slice();
res = data.concat();
res = [];
data.forEach(element => res.push(element));
res = [];
for (let i = 0; i < data.length; i++) {
res.push(data[i]);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
slice | |
concat | |
forEach | |
Traditional For Loop |
Test name | Executions per second |
---|---|
slice | 4309205.5 Ops/sec |
concat | 3314342.5 Ops/sec |
forEach | 4537916.5 Ops/sec |
Traditional For Loop | 4854953.0 Ops/sec |
Let's break down the provided benchmark and its test cases.
Benchmark Overview
The benchmark measures the performance of shallow copy operations on an array. The input data is fetched from a JSON endpoint using the fetch
API, which returns a JSON response. This response is then assigned to the data
variable. The goal is to create a shallow copy of this data and compare the performance of different methods.
Script Preparation Code
The script preparation code fetches data from the specified JSON endpoint using the fetch
API:
var data = [];
var res = [];
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(json => data = json);
This code uses the fetch
API to make a GET request to the specified endpoint, which returns a JSON response. The response is then parsed as JSON and assigned to the data
variable.
Html Preparation Code
There is no HTML preparation code provided for this benchmark.
Test Cases
The test cases are defined in the individual test cases array:
[
{
"Benchmark Definition": "res = data.slice();",
"Test Name": "slice"
},
{
"Benchmark Definition": "res = data.concat();",
"Test Name": "concat"
},
{
"Benchmark Definition": "res = [];\r\ndata.forEach(element => res.push(element));",
"Test Name": "forEach"
},
{
"Benchmark Definition": "res = [];\r\nfor (let i = 0; i < data.length; i++) {\r\n\tres.push(data[i]);\r\n}",
"Test Name": "Traditional For Loop"
}
]
Each test case defines a different method for creating a shallow copy of the data
array:
data
array using the slice()
method.data
array using the concat()
method.data
array and pushing it to a new array using the forEach()
method.data
array using a traditional for loop and pushing it to a new array.Library: None
There is no library used in this benchmark. The only library mentioned is fetch
, which is part of the browser's native API.
Special JS Feature or Syntax: None
There are no special JavaScript features or syntax used in this benchmark.
Benchmark Results
The latest benchmark results show the performance of each test case:
Test Name | ExecutionsPerSecond |
---|---|
Traditional For Loop | 4854953.0 |
forEach | 4537916.5 |
slice | 4309205.5 |
concat | 3314342.5 |
These results indicate that the traditional for loop method is the fastest, followed closely by the slice()
method. The forEach()
method and the concat()
method are slower.
Other Alternatives
If you're interested in exploring alternative shallow copy methods, here are a few options:
These alternatives can be used to create shallow copies, but they may have different performance characteristics compared to the methods tested in this benchmark.