Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array mutation VS creation
(version: 0)
Comparing performance of:
new array vs mutation
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
new array
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const arr1 = [0, ...arr];
mutation
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; arr.unshift(0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
new array
mutation
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 break down the provided JSON data and explain what's being tested. **Benchmark Definition** The benchmark is defined by two test cases: 1. **"new array"**: This test case measures the performance of creating a new array using the spread operator (`[0, ...arr]`) versus creating an empty array and then pushing elements into it. 2. **"mutation"**: This test case measures the performance of modifying an existing array by calling the `unshift` method. **Options compared** In each test case, the following options are being compared: * For "new array": + Creating a new array using the spread operator (`[0, ...arr]`) + Creating an empty array and then pushing elements into it (using `[]` followed by `.push(arr)` or `.concat(arr)`) * For "mutation": + Modifying an existing array using the `unshift` method + Creating a new array and then assigning it to a variable **Pros and Cons** Here are some pros and cons of each approach: For "new array": * Using the spread operator (`[0, ...arr]`) has the advantage of being more concise and readable. It also avoids creating an intermediate array. * However, this method can be slower for large arrays due to the overhead of creating a new array object. Creating an empty array and then pushing elements into it: * This approach is generally faster than using the spread operator because it avoids creating an additional array object. * However, it requires more code and may be less readable. For "mutation": * Using the `unshift` method can be faster for large arrays because it modifies the existing array in-place without creating a new one. * However, this approach requires modifying the original array, which might not be desirable in all cases. * Creating a new array and then assigning it to a variable: + This approach is generally slower than using `unshift` because it creates an additional array object. + However, it avoids modifying the original array. **Libraries and special JS features** None of the test cases use any external libraries. The only special JavaScript feature being tested here is the spread operator (`[0, ...arr]`) introduced in ECMAScript 2015 (ES6).
Related benchmarks:
Creation of new Array: Array.from vs. new Array
Array.from() vs new Array() + destruct
Array.from() vs new Array() - empty
Array.from({ length: n }) vs new Array(n)
Array.from() vs new A
Comments
Confirm delete:
Do you really want to delete benchmark?