Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
replace an array in JavaScript - splice vs push vs for
(version: 0)
Based on question on http://stackoverflow.com/questions/1232040/how-do-i-empty-an-array-in-javascript
Comparing performance of:
push vs splice vs for
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var size = 10000; var arr1 = []; var arr2 = []; for (var i = 0; i < size; i++) { arr1.push(i); arr2.push(i); }
Tests:
push
arr2.length = 0; arr2.push(...arr1);
splice
arr2.splice(0, arr2.length, ...arr1);
for
if (arr2.length !== arr1.length) arr2.length = arr1.length; for (let i = 0, length = arr1.length; i < length; ++i) { arr2[i] = arr1[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
push
splice
for
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:145.0) Gecko/20100101 Firefox/145.0
Browser/OS:
Firefox 145 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
push
42226.1 Ops/sec
splice
4628.0 Ops/sec
for
82308.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what is being tested. **Benchmark Overview** The benchmark tests three different approaches to replace an array in JavaScript: 1. Using `push()` method 2. Using `splice()` method 3. Using a manual loop (for) **Library Used** There is no specific library used in this benchmark, only built-in JavaScript functions and operators. **Special JS Feature/Syntax** None mentioned explicitly, but note that the use of `let` keyword for variable declaration (`"for": { ... "for": { let i = 0; ... } }`) suggests a recent JavaScript feature introduced with ECMAScript 2015 (ES6). However, since this benchmark is not specifically testing this feature, we'll ignore it. **Benchmark Test Cases** The benchmark consists of three test cases: 1. **Push**: The first test case creates two arrays `arr1` and `arr2`, populates them with numbers from 0 to 9999 (10000 iterations), and then uses the `push()` method to add all elements of `arr1` to `arr2`. The goal is to see which approach takes less time. 2. **Splice**: The second test case creates two arrays `arr1` and `arr2`, populates them with numbers from 0 to 9999 (10000 iterations), and then uses the `splice()` method to replace all elements of `arr2` with those of `arr1`. The goal is to see which approach takes less time. 3. **For**: The third test case creates two arrays `arr1` and `arr2`, populates them with numbers from 0 to 9999 (10000 iterations), and then uses a manual loop to assign elements from `arr1` to `arr2`. The goal is to see which approach takes less time. **Options Compared** The benchmark compares the performance of three different approaches: * **Push**: Using the `push()` method to add elements to an array. * **Splice**: Using the `splice()` method to replace elements in an array. * **For**: Using a manual loop to assign elements from one array to another. **Pros and Cons** Here are some pros and cons of each approach: * **Push**: + Pros: Simple, efficient, and widely supported. It uses the least amount of memory (no need to create a new array). + Cons: May not be suitable for large arrays or arrays with many duplicates. * **Splice**: + Pros: Allows for more control over the replacement process. Can be used for both inserting and removing elements. + Cons: Uses more memory than `push()` since it needs to create a new array. Also, can be slower due to the overhead of creating and manipulating arrays. * **For**: + Pros: Provides fine-grained control over the assignment process. Suitable for large arrays or arrays with many duplicates. + Cons: Can be slow due to the overhead of the loop. Requires manual memory management. **Other Alternatives** There are other alternatives that could be used to replace an array in JavaScript, such as: * Using `concat()`: Concatenating two arrays using the `concat()` method. * Using `set()`: Using the `Set` data structure to store unique elements from one array and then converting it back to an array using `Array.from()`. * Using `reduce()`: Using the `reduce()` method to combine elements from two arrays into a single array. However, these alternatives may have different performance characteristics or trade-offs compared to the approaches tested in this benchmark.
Related benchmarks:
empty an array in JavaScript - splice vs setting length
empty an array in JavaScript - splice vs setting length. 444
empty an array in JavaScript - splice vs setting length faster
Array.splice(0, N) vs Array.length === N
Comments
Confirm delete:
Do you really want to delete benchmark?