var numbers = [Array(1000).keys()]
var arr = [numbers, numbers, numbers, numbers, numbers]
var results = arr.reduce((acc, cur) => acc.concat(cur), []);
var results = arr.reduce((acc, cur) => [acc, cur], []);
var results = arr.reduce((acc, cur) => {
acc.push(cur);
return acc;
}, []);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
concat | |
spread operator | |
push with spread operator |
Test name | Executions per second |
---|---|
concat | 167493.3 Ops/sec |
spread operator | 40211.1 Ops/sec |
push with spread operator | 87895.8 Ops/sec |
Let's break down the provided JSON benchmark data and explain what is being tested.
Benchmark Definition: The benchmark defines three test cases, each measuring the performance of a different approach to concatenate arrays:
concat
method with the spread operator (...
) on an array....
) to concatenate two arrays.push
method with the spread operator (...
) to add elements to an existing array.Script Preparation Code: The script prepares an array of numbers and creates a main array by repeating it five times:
var numbers = [...Array(1000).keys()]
var arr = [numbers, numbers, numbers, numbers, numbers]
This array has 5000 elements, which will be used for the benchmark.
Test Cases: The three test cases are defined as follows:
concat
method.var results = arr.reduce((acc, cur) => acc.concat(cur), [])
...
) to concatenate two arrays.var results = arr.reduce((acc, cur) => [...acc, ...cur], [])
push
method with the spread operator (...
) to add elements to an existing array.var results = arr.reduce((acc, cur) => {
acc.push(...cur);
return acc;
}, [])
Library and Purpose: The benchmark uses built-in JavaScript methods and features, without any external libraries.
Special JS Features or Syntax: None of the test cases use special JavaScript features or syntax that would affect performance in a significant way. The benchmark focuses on comparing the performance of different array concatenation approaches.
Other Considerations:
ExecutionsPerSecond
) for each test case.Alternatives:
Array.prototype.reduce
or custom loops, could be tested to compare their performance.By understanding the test cases, script preparation code, and library used, you can better comprehend the benchmark's purpose and performance characteristics.