Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.concat vs Spread speedtest
(version: 2)
Comparing performance of:
Array.prototype.concat vs Spread operator
Created:
7 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function generateRandomNumbers(m) { const array = new Uint32Array(m); window.crypto.getRandomValues(array); return Array.prototype.map.call(array, val => val); } function generateRandomArrays(n, m) { const array = []; for (let i = 0; i < n; i++) { array.push(generateRandomNumbers(m)) } return array; } var array = generateRandomArrays(1e4, 1e1);
Tests:
Array.prototype.concat
const b = array.reduce((accumulator, value) => accumulator.concat(value));
Spread operator
const c = array.reduce((accumulator, value) => [...accumulator, ...value]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.prototype.concat
Spread operator
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON benchmark definition and its test cases. **What is tested?** The benchmark tests two different ways to concatenate arrays in JavaScript: using `Array.prototype.concat()` and using the spread operator (`...`). **Options compared** There are two options being compared: 1. **`Array.prototype.concat()`**: This method creates a new array by copying elements from an existing array. 2. **Spread operator (`...`)**: This syntax creates a new array by spreading elements from an existing array. **Pros and Cons of each approach** * **`Array.prototype.concat()`**: + Pros: - More explicit and readable code - Can be more efficient for large arrays, as it avoids creating temporary arrays + Cons: - Creates a new array, which can lead to higher memory usage - Can be slower than the spread operator for small arrays * **Spread operator (`...`)**: + Pros: - More concise and readable code - Avoids creating temporary arrays, making it more memory-efficient + Cons: - Less explicit than `concat()`, which can make it harder to understand - Can be slower than `concat()` for large arrays **Library used** The benchmark uses the `reduce()` method of arrays, which is a common way to concatenate arrays in JavaScript. The `reduce()` method applies a function to each element in an array and returns a single value. No external libraries are required for this benchmark. **Special JS feature or syntax** The spread operator (`...`) is a relatively recent addition to the JavaScript language, introduced in ECMAScript 2015 (ES6). It allows you to create a new array by spreading elements from an existing array. **Benchmark preparation code** The provided `generateRandomNumbers` and `generateRandomArrays` functions are used to generate random arrays for testing. These functions use the `window.crypto.getRandomValues()` method to generate random numbers, which is a widely supported feature in modern browsers. **Other alternatives** For concatenating arrays, you can also use other methods like: * Using the `Array.prototype.push()` method and setting `length` property manually * Creating an intermediate array using `Array.from()` or `new Array()`, and then pushing elements to it However, these approaches may be less efficient or more verbose than using `concat()` or the spread operator. **Test case interpretation** The test cases are designed to measure the performance of concatenating arrays in two different ways. The raw UA string data provides information about the browser, device platform, and operating system being used for each test run. The `ExecutionsPerSecond` value indicates how many iterations of the benchmark were completed per second on the given hardware. The benchmark results show that Chrome 87 is faster at concatenating arrays using the spread operator than using `concat()`. This may be due to various factors, such as the language implementation, browser optimizations, or even hardware-specific differences.
Related benchmarks:
Array.prototype.concat vs spread operator - Immutable version
Array concat vs spread operator vs push with random array 10000
Spread operator vs Array.prototype.concat() for large arrays
Spread Operator VS Array.prototype.slice() VS Array.prototype.slice(0)
Spread Operator VS Array.prototype.slice() VS Array.prototype.map()
Comments
Confirm delete:
Do you really want to delete benchmark?