Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
empty an array in JavaScript?(Yorkie)1
(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
for (var e = 0; e < 100000; e++){ let size = 100; let arr1 = []; for (var i = 0; i < size; i++){ arr1.push(i); } arr1 = []; }
Set length to zero
for (var e = 0; e < 100000; e++){ let size = 100; let arr2 = []; for (var i = 0; i < size; i++){ arr2.push(i); } arr2.length = 0; }
Splice
for (var e = 0; e < 100000; e++){ let size = 100; let arr3 = []; for (var i = 0; i < size; i++){ arr3.push(i); } arr3.splice(0, arr3.length); }
Pop all values
for (var e = 0; e < 100000; e++){ let size = 100; let 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):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Description** The benchmark is designed to measure the performance of three different approaches for emptying an array in JavaScript: 1. Instantiating a new, empty array (`let arr = []`) 2. Setting the length of an existing array to zero (`arr.length = 0`) 3. Using `splice(0, arr.length)` to remove all elements from an array 4. Popping all elements from an array using a while loop (`while(arr.length > 0) { arr.pop(); }`) **Options Comparison** The benchmark compares the performance of these four approaches on a large array of size 100,000 elements. * **Pros and Cons:** + Instantiating a new array: - Pros: Simple, efficient, and creates a fresh copy of the array. - Cons: Can be slower than other methods if the array is very large or sparse. + Setting length to zero: - Pros: Fast, as it only updates the internal reference count of the array. - Cons: May trigger garbage collection, which can slow down performance. + Splice: - Pros: Can be faster than setting length to zero for large arrays with many elements. - Cons: Creates a new array object and may trigger garbage collection. + Popping all values: - Pros: Simple and easy to implement. - Cons: May not be the most efficient approach, as it uses more iterations than other methods. **Library Usage** None of the benchmark definitions use any external libraries or frameworks. The focus is solely on measuring the performance of built-in JavaScript features. **Special JS Features/Syntax** The benchmark definition `splice(0, arr.length)` uses the `splice()` method with two arguments: the starting index (0) and the number of elements to remove (`arr.length`). This syntax is a shorthand way to remove all elements from an array. **Other Alternatives** If you want to test alternative approaches for emptying an array in JavaScript, some other options include: * Using `Array.prototype.fill(0)` or `Array.from(new Array(size).fill(0))` to create a new array with all elements set to 0. * Using `let arr = new Int32Array(size);` and then setting all elements to 0 using `arr.fill(0)`. * Using a custom implementation, such as a loop that iterates over the array and assigns each element to a variable. Keep in mind that these alternatives may have different performance characteristics depending on the specific use case and browser version.
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?