Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Add new element to large array (preperation): push vs destructuring vs bracket access
(version: 0)
Comparing performance of:
spread operator vs Push vs bracket access
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var params = [ "hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true,"hello", true, 7 ]
Tests:
spread operator
var other = [ ...params, 'new' ]
Push
params.push('new');
bracket access
params[ params.length ] = 'new';
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
spread operator
Push
bracket access
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 and explain what's being tested, compared, and considered in each test case. **Benchmark Definition** The benchmark is testing three different approaches to adding a new element to a large array: 1. **Push**: Using the `push()` method to add an element to the end of the array. 2. **Destructuring**: Using the spread operator (`...`) to create a new array with the existing elements and the new one appended. 3. **Bracket access**: Accessing the last index of the array using bracket notation (`params[ params.length ] = 'new'`). **Options Compared** The three approaches are compared in terms of performance, specifically: * Execution time (measured in executions per second) * Browser-specific optimizations and cache effects **Pros and Cons of Each Approach** 1. **Push**: * Pros: Fast and efficient, as it doesn't require creating a new array. * Cons: May cause issues if the array is large and the `push()` method has to traverse the entire array to find the correct index. 2. **Destructuring (Spread Operator)**: * Pros: Creates a new array with a clear separation between existing elements and the new one, making it easier to debug. * Cons: May be slower than `push()` due to the overhead of creating a new array. 3. **Bracket access**: * Pros: Simple and concise, as it directly accesses the last index of the array. * Cons: May cause issues if the array is large or has many elements, as it requires calculating the index manually. **Library Used** The `push()` method uses the JavaScript built-in `Array.prototype.push()` method. No specific library is required for this test case. **Special JS Feature/Syntax** None mentioned in the provided code. **Benchmark Preparation Code** The preparation code creates a large array `params` with 20 elements, including some strings and booleans, and then defines a new variable `other` to hold the modified array. The code is: ```javascript var params = [ "hello", true, "hello", true, ...params, 'new' ]; ``` This creates a copy of the original `params` array using destructuring, appends the new element `'new'`, and assigns it to the new variable `other`. **Individual Test Cases** Each test case defines a different variant of the benchmark: 1. **Spread operator**: Uses the spread operator (`...`) to create a new array with the existing elements and the new one appended. ```javascript var other = [ ...params, 'new' ]; ``` 2. **Push**: Uses the `push()` method to add an element to the end of the array. ```javascript params.push('new'); ``` 3. **Bracket access**: Accesses the last index of the array using bracket notation (`params[ params.length ] = 'new'`). ```javascript params[ params.length ] = 'new'; ``` **Latest Benchmark Result** The latest benchmark result shows the execution time for each test case on a desktop Firefox 105 browser: 1. **Push**: 21885012 executions per second 2. **Bracket access**: 16182047 executions per second 3. **Spread operator**: 562220.875 executions per second These results suggest that `push()` is the fastest approach, followed by bracket access, and then destructuring (spread operator).
Related benchmarks:
Add new element to large array: push vs destructuring vs bracket access
Add new element to very large array: push vs destructuring vs bracket access
Add new element to very large array (preparation): push vs destructuring vs bracket access
Add new element to array: push vs destructuring, with longer array
Comments
Confirm delete:
Do you really want to delete benchmark?