Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
testing array ops
(version: 1)
Comparing performance of:
... vs append
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
function generateArray(n) { const ar = []; for (let i = 0; i < n; i++) { ar.push(Math.floor(Math.random() * 100)); } return ar } var arrayEl = []; var a = generateArray(100); var b = generateArray(100); var c = generateArray(100);
Tests:
...
arrayEl = []; arrayEl = [...a, ...b, ...c];
append
arrayEl = []; arrayEl.concat(a); arrayEl.concat(b); arrayEl.concat(c);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
...
append
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
...
831299.1 Ops/sec
append
4403239.5 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark provided tests the performance of different methods for combining arrays in JavaScript. This is particularly relevant in scenarios where performance is a concern when manipulating large datasets. ### Description of Comparison 1. **Spread Operator Method:** - **Benchmark Definition:** `arrayEl = [];\r\narrayEl = [...a, ...b, ...c];` - **Test Name:** Not explicitly defined. - **Description:** This method uses the spread operator (`...`) to combine the arrays `a`, `b`, and `c` into a new array. This is a modern and syntactically concise way to concatenate arrays. 2. **Array.concat Method:** - **Benchmark Definition:** `arrayEl = [];\r\narrayEl.concat(a);\r\narrayEl.concat(b);\r\narrayEl.concat(c);` - **Test Name:** "append". - **Description:** This method utilizes the `concat` method of the Array prototype to append arrays `a`, `b`, and `c` to an initially empty array `arrayEl`. Each call to `concat` does not mutate the original array but returns a new array that combines the input arrays. ### Pros and Cons #### Spread Operator (`...`) - **Pros:** - Concise syntax; easier to read and write. - Enables you to spread not just arrays but also iterable objects (like strings). - Suitable for one-liners, making it ideal for quick operations. - **Cons:** - Can lead to performance issues when dealing with extremely large arrays, as it creates a "new" array with each operation. - Each set of spread operations creates more intermediate arrays, which could potentially lead to higher memory usage. #### `Array.concat` - **Pros:** - More traditional and widely understood in terms of array manipulation. - It can concatenate multiple arrays in a single call when provided with multiple arguments. - Each call to `concat` doesn’t mutate the original array, which can lead to safer manipulations of data structures. - **Cons:** - More verbose than the spread operator. - Can be less intuitive at first glance compared to the simplicity of the spread syntax. - As with the spread operator, excessive use with large arrays can result in performance drawbacks due to repeated array creation. ### Performance Considerations The benchmark measures the `ExecutionsPerSecond` to assess performance efficiency. In the test results, it was noted that the `Array.concat` method performs significantly faster than the spread operator method in the tested environment. ### Alternatives 1. **Using `push` with `apply`:** ```javascript Array.prototype.push.apply(arrayEl, a); Array.prototype.push.apply(arrayEl, b); Array.prototype.push.apply(arrayEl, c); ``` - This would be an alternative to both methods above, where `apply` is used to push multiple elements from each array into `arrayEl`, which can sometimes perform better than both concat and spread for large datasets, as it avoids the creation of multiple intermediate arrays. 2. **Using Loops:** - Manually looping through arrays and inserting elements, though this method is often less efficient due to JavaScript engine optimizations that apply to built-in methods. 3. **Array.from():** - You could also use the `Array.from()` method to create a new array from an iterable or array-like object. In summary, the benchmark compares the performance implications of using modern JavaScript syntax versus traditional methods in array manipulations, allowing developers to make informed decisions based on their performance needs.
Related benchmarks:
Array population test
Array population test
Array loops
Array loops
loopings
loops-perf
Array Entries vs fori
at vs index
JS array for test
Comments
Confirm delete:
Do you really want to delete benchmark?