Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.concat vs spread vs push 08072019
(version: 0)
Comparing performance of:
Array.prototype.concat vs spread operator vs Push
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
Array.prototype.concat
var params = [ "hello", true, 7 ]; var other = params.concat(1);
spread operator
var params = [ "hello", true, 7 ]; var other = [...params, 1];
Push
var params = [ "hello", true, 7 ]; params.push(1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.prototype.concat
spread operator
Push
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 world of JavaScript microbenchmarks on MeasureThat.net. The provided benchmark measures the performance difference between three approaches to add an element to an array: 1. `Array.prototype.concat()` 2. Spread operator (`...`) 3. `Array.prototype.push()` method **What is being tested?** Each test case creates an array `params` with a few elements and then attempts to add another element to it using one of the three approaches. The benchmark measures the execution time (in executions per second) for each approach. **Options compared:** * `Array.prototype.concat()`: This method returns a new array and does not modify the original array. * Spread operator (`...`): This operator creates a new array by copying the elements from the original array and adding the new element to it. * `Array.prototype.push()` method: This method modifies the original array in-place, adding the new element at the end. **Pros and Cons of each approach:** 1. **`Array.prototype.concat()`**: * Pros: Does not modify the original array, can be useful when you need a copy of the array. * Cons: Returns a new array, which may incur additional memory allocation overhead. Can be slower than `push()` due to the creation of a new array object. 2. **Spread operator (`...`)**: * Pros: Creates a new array without modifying the original one, can be more concise and readable. * Cons: May incur additional memory allocation overhead for creating a new array, can be slower than `push()` if the new element is large or the original array is very small. 3. **`Array.prototype.push()` method**: * Pros: Modifies the original array in-place, typically faster and more efficient than `concat()` or spread operator. * Cons: Modifies the original array, may be less readable for some developers. **Library/Utility functions used:** None of the benchmark tests use external libraries or utility functions that would affect the outcome of the benchmark. **Special JS feature/syntax:** The benchmark uses a recent JavaScript feature: the spread operator (`...`). The spread operator is supported in most modern browsers, including Chrome 74, which is the browser used for this benchmark. If your target audience includes older browsers or environments that do not support the spread operator, you may need to modify the test cases. **Other alternatives:** If you want to include other approaches to add an element to an array, such as: * Using `Array.prototype.splice()` * Creating a new array using `Array.from()` or `Array.of()` * Using a library like Lodash's `_.concat()` or `_.spread()` You can modify the test cases to include these alternatives. However, keep in mind that the benchmark may need to be updated accordingly to accurately measure their performance. In conclusion, this benchmark measures the performance difference between three common approaches to add an element to an array: `Array.prototype.concat()`, spread operator (`...`), and `Array.prototype.push()`. Understanding the pros and cons of each approach can help you choose the most efficient method for your specific use case.
Related benchmarks:
Array spread vs. push performance
concat 2 arrays: Array.prototype.concat vs spread operator
Array.prototype.concat vs spread operator (add)
Array.prototype.concat vs spread operator (fix)
Array concat vs spread operator vs push + spread 2023-08-21
Comments
Confirm delete:
Do you really want to delete benchmark?