Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
push vs spread
(version: 0)
Comparing performance of:
push vs spread
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var test1=Array.from({length: 10000},()=>Math.random()), test2 = [...test1]
Tests:
push
test1.push(123)
spread
[...test2, 123]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
push
spread
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 explaining the benchmark. **What is being tested?** The provided benchmark compares two different ways of appending an element to an array in JavaScript: using the `push()` method and the spread operator (`...`). **Options compared** There are two test cases: 1. **`test1.push(123)`**: This test case uses the `push()` method to append a number (123) to the end of an array (`test1`). The array is created using `Array.from()`, which generates a new array with 10,000 random elements. 2. **`[...test2, 123]`**: This test case uses the spread operator (`...`) to create a new array by copying the existing array (`test2`) and appending the number (123) to it. **Pros and Cons of each approach** 1. **`push()` method**: * Pros: Faster for large arrays, as it modifies the original array in-place. * Cons: Can be slower for small arrays or when creating a new array is more efficient. 2. **Spread operator (`...`)**: * Pros: More concise and readable, especially for creating new arrays from existing ones. * Cons: Creates a new array every time it's used, which can lead to performance issues with large datasets. **Library usage** In this benchmark, the `Array.from()` function is used to create an initial array (`test1`). This function is part of the ECMAScript Standard Library and is widely supported by modern browsers and JavaScript engines. **Special JS feature or syntax** None are mentioned in this specific benchmark. However, if you're interested in exploring other JavaScript features or syntax, I can provide information on various topics. **Other alternatives** If you want to explore alternative approaches, here are a few examples: * Using `concat()` instead of the spread operator: `[...test2].concat(123)` * Creating an array using a loop instead of `Array.from()`: `var test1 = []; for (var i = 0; i < 10000; i++) { test1.push(Math.random()); }` * Using a library like Lodash's `repeat()` function to create the initial array: `const _ = require('lodash'); var test1 = _.repeat(Array(10000), Math.random);` Keep in mind that these alternatives may have different performance characteristics and may not be as concise or readable as the original code.
Related benchmarks:
Arrays: spread operator vs push
Push to array, vs ES6 Spread.
array update push vs spread
JS array spread operator vs push
Comments
Confirm delete:
Do you really want to delete benchmark?