Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array Spread vs Array concat (with object dataset)
(version: 0)
Comparing performance of:
Array Concat (w/ objects) vs Spread (w/ objects)
Created:
7 years ago
by:
Registered User
Jump to the latest result
Tests:
Array Concat (w/ objects)
const arr1 = [{ "name": "Field1", "width": 20, "boolProp": false, "visible": true, "id": 1 }, { "name": "Field2", "width": 10, "boolProp": false, "visible": true, "id": 2 }, { "name": "Field3", "width": 20, "boolProp": false, "visible": true, "id": 3 }, { "name": "Field4", "width": 10, "boolProp": false, "visible": true, "id": 4 }]; const arr2 = [{ "name": "Field5", "width": 20, "boolProp": false, "visible": true, "id": 1 }, { "name": "Field6", "width": 10, "boolProp": false, "visible": true, "id": 2 }, { "name": "Field7", "width": 20, "boolProp": false, "visible": true, "id": 3 }, { "name": "Field8", "width": 10, "boolProp": false, "visible": true, "id": 4 }]; const arr3 = arr1.concat(arr2);
Spread (w/ objects)
const arr1 = [{ "name": "Field1", "width": 20, "boolProp": false, "visible": true, "id": 1 }, { "name": "Field2", "width": 10, "boolProp": false, "visible": true, "id": 2 }, { "name": "Field3", "width": 20, "boolProp": false, "visible": true, "id": 3 }, { "name": "Field4", "width": 10, "boolProp": false, "visible": true, "id": 4 }]; const arr2 = [{ "name": "Field5", "width": 20, "boolProp": false, "visible": true, "id": 1 }, { "name": "Field6", "width": 10, "boolProp": false, "visible": true, "id": 2 }, { "name": "Field7", "width": 20, "boolProp": false, "visible": true, "id": 3 }, { "name": "Field8", "width": 10, "boolProp": false, "visible": true, "id": 4 }]; const arr3 = [...arr1, ...arr2];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array Concat (w/ objects)
Spread (w/ objects)
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 dive into the benchmark definition and test cases. **Benchmark Definition** The benchmark measures the performance difference between using the spread operator (`...`) to create a new array by copying elements from an existing array, versus using the `concat()` method to achieve the same result. The benchmark is defined for objects with properties, specifically comparing the execution times of `arr1.concat(arr2)` and `[...arr1, ...arr2]`. **Options Compared** The two options being compared are: 1. **Array Concat (w/ objects)**: This option uses the `concat()` method to create a new array by concatenating two arrays. 2. **Spread (w/ objects)**: This option uses the spread operator (`...`) to create a new array by copying elements from an existing array. **Pros and Cons** * **Array Concat**: + Pros: Wide support across browsers, well-established API. + Cons: Can be slower than the spread operator due to the overhead of concatenating arrays. * **Spread Operator**: + Pros: Fast and efficient, especially for large datasets. Also, reduces code duplication by avoiding the need to use `concat()`. + Cons: Requires support for ECMAScript 2018 (ES2018) feature. **Library and Purpose** None mentioned in the benchmark definition. **Special JS Feature/Syntax** The benchmark uses the spread operator (`...`), which is a JavaScript feature introduced in ECMAScript 2018 (ES2018). The spread operator allows you to copy elements from an array by placing `...` before it, like `[...arr1, ...arr2]`. **Other Considerations** * **Performance**: As mentioned earlier, the spread operator is generally faster than using `concat()`, especially for large datasets. * **Code Duplication**: The spread operator eliminates the need to use `concat()` in favor of a more concise and readable syntax. **Alternatives** If you can't use ECMAScript 2018 (ES2018), alternative approaches include: 1. Using `slice()` method: `[arr1.slice(), arr2]` 2. Using `Array.prototype.push()`: `arr1.concat(arr2)` However, these alternatives may not be as efficient or readable as the spread operator. Overall, the benchmark highlights the benefits of using the spread operator for creating new arrays from existing ones, particularly in scenarios where performance and code readability are critical.
Related benchmarks:
concat vs unshift vs spread
simple spread vs concat benchmark
unshift vs spread vs concat
Concat vs Spread for Large Arrayss
spread vs concat for cloning array benchmark
Comments
Confirm delete:
Do you really want to delete benchmark?