Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Arrays: spread operator vs push
(version: 0)
Comparing performance of:
spread operator vs push function
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
spread operator
var arr = [1, 2, 3]; var out = [...arr, 4];
push function
var arr = [1, 2, 3]; arr.push(4);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
spread operator
push function
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:137.0) Gecko/20100101 Firefox/137.0
Browser/OS:
Firefox 137 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
spread operator
34487384.0 Ops/sec
push function
148305088.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two ways to append elements to an array in JavaScript: using the spread operator (`...`) and the `push()` function. **Options Compared** 1. **Spread Operator**: The spread operator (`...`) is used to create a new array by spreading the elements of an existing array into a new array. 2. **Push Function**: The `push()` function is used to add one or more elements to the end of an array. **Pros and Cons** * **Spread Operator:** + Pros: - Creates a new array, which can be more efficient than modifying the original array. - Less error-prone, as it doesn't involve mutation of the original array. + Cons: - Can be slower due to the creation of a new array. * **Push Function:** + Pros: - Faster, as it modifies the existing array without creating a new one. - Less memory-intensive. + Cons: - Can lead to errors if not used carefully (e.g., modifying the original array instead of the expected array). **Library and Special JavaScript Features** There is no library or special JavaScript feature explicitly mentioned in this benchmark. **Other Considerations** * The benchmark assumes that the input arrays are large enough to require significant performance differences between the two methods. * The benchmark results may not reflect real-world scenarios, where code readability, maintainability, and other factors might be more important than performance. **Alternatives** If you're looking for alternative ways to append elements to an array, some options include: 1. Using `concat()`: This method creates a new array by concatenating the elements of two or more arrays. 2. Using `set()` (in modern browsers): This method sets the value of a property on a Map object with a specified key and a specified value. Keep in mind that these alternatives might have different performance characteristics, trade-offs, and use cases compared to the spread operator and push function. I hope this explanation helps! Let me know if you have any further questions.
Related benchmarks:
Array: spread operator vs push
Pushing items via Array.push vs. Spread Operator
Array.push vs Spread operator
JS array spread operator vs push
Array .push() vs spread operator
Comments
Confirm delete:
Do you really want to delete benchmark?