Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
empty an array in JavaScript?(Yorkie)
(version: 0)
Based on question on http://stackoverflow.com/questions/1232040/how-do-i-empty-an-array-in-javascript
Comparing performance of:
Just instantiate new array vs Set length to zero vs Splice vs Pop all values
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
Just instantiate new array
var size = 1000000; var arr1 = []; for (var i = 0; i < size; i++){ arr1.push(i); } arr1 = [];
Set length to zero
var size = 1000000; var arr2 = []; for (var i = 0; i < size; i++){ arr2.push(i); } arr2.length = 0;
Splice
var size = 1000000; var arr3 = []; for (var i = 0; i < size; i++){ arr3.push(i); } arr3.splice(0, arr3.length);
Pop all values
var size = 1000000; var arr4 = []; for (var i = 0; i < size; i++){ arr4.push(i); } while(arr4.length > 0){ arr4.pop(); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Just instantiate new array
Set length to zero
Splice
Pop all values
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):
**What is tested on the provided JSON?** The provided JSON represents four benchmark test cases that compare different approaches to empty an array in JavaScript. The test cases are: 1. **Just instantiate new array**: Create a new array and immediately assign it to `arr1`, then delete all elements from `arr1`. 2. **Set length to zero**: Set the length of `arr2` to 0, which also deletes all elements from `arr2`. 3. **Splice**: Use the `splice()` method to remove all elements from `arr3`. 4. **Pop all values**: Use a while loop with `pop()` to delete all elements from `arr4`. **Options compared** Each test case compares two different approaches: * Just instantiate new array vs. Set length to zero * Splice vs. Pop all values **Pros and Cons of each approach** 1. **Just instantiate new array (Test 1) vs. Set length to zero (Test 2)** * Pros: + Fastest execution time, as it simply reassigns the array reference. + Least memory allocation, as no new objects are created. * Cons: + May not be the most intuitive approach, as it relies on assigning a new array object to `arr1`. 2. **Set length to zero (Test 2)** * Pros: + More intuitive than reassigning an array reference. * Cons: + May incur some overhead due to array resizing operations. 3. **Splice (Test 3)** * Pros: + Flexibility, as it can remove any number of elements from the array. * Cons: + Slower execution time compared to reassigning an array reference or setting length to zero. 4. **Pop all values (Test 4)** * Pros: + Simple and straightforward approach. * Cons: + May be slower than other approaches, as it involves repeated calls to `pop()`. **Library** None of the provided benchmark test cases use any external libraries. However, JavaScript's built-in array methods (e.g., `splice()`, `length`) are used in each test case. **Special JS feature or syntax** No special features or syntax are mentioned in the provided JSON. The tests only use standard JavaScript syntax and built-in array methods. **Other alternatives** Some alternative approaches to empty an array in JavaScript include: * Using the `fill()` method with a length of 0: `arr.fill(0, 0, arr.length);` * Using `Array.prototype.fill()` with a length of 0: `new Array(arr.length).fill(0);` These alternatives may offer performance benefits or more intuitive approaches than the ones tested in this benchmark. Keep in mind that benchmarking JavaScript performance can be complex and influenced by various factors, such as browser-specific optimizations and hardware.
Related benchmarks:
empty an array in JavaScript?
Test if array is empty
empty an array in JavaScript - splice vs setting length faster
check if array is empty or not using length and at method
Empty an array in JavaScript
Comments
Confirm delete:
Do you really want to delete benchmark?