Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
deleting array in Js
(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 vs delete
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
Just instantiate new array
var size = 10000; var arr1 = []; for (var i = 0; i < size; i++){ arr1.push(i); } arr1 = [];
Set length to zero
var size = 10000; var arr2 = []; for (var i = 0; i < size; i++){ arr2.push(i); } arr2.length = 0;
Splice
var size = 10000; var arr3 = []; for (var i = 0; i < size; i++){ arr3.push(i); } arr3.splice(0, arr3.length);
Pop all values
var size = 10000; var arr4 = []; for (var i = 0; i < size; i++){ arr4.push(i); } while(arr4.length > 0){ arr4.pop(); }
delete
var size = 10000; var arr4 = []; for (var i = 0; i < size; i++){ arr4.push(i); } delete arr4;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Just instantiate new array
Set length to zero
Splice
Pop all values
delete
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 being tested?** MeasureThat.net is testing the performance of deleting an array in JavaScript. The benchmark definition specifies four different approaches to delete an array: 1. Instantiating a new empty array 2. Setting the length of an existing array to zero 3. Using `splice(0, arr3.length)` to remove all elements from an array 4. Popping all values from an array using a while loop **Options compared** The benchmark compares the performance of these four approaches: * **Just instantiate new array**: Creating a new empty array and assigning it to a variable. * **Set length to zero**: Setting the length property of an existing array to zero. * **Splice**: Using `splice(0, arr3.length)` to remove all elements from an array. * **Pop all values**: Popping all values from an array using a while loop. **Pros and Cons** Here are some pros and cons for each approach: 1. **Just instantiate new array** * Pros: Simple and efficient way to create a new empty array. * Cons: Creates a new object every time, which can lead to memory allocation overhead. 2. **Set length to zero** * Pros: Only modifies the existing array, reducing memory allocation overhead. * Cons: Can be slower than other methods due to the need to update the array's internal length property. 3. **Splice** * Pros: Efficient way to remove all elements from an array by modifying the array's internal length property. * Cons: Requires a second argument (the count of elements to remove) and can lead to unexpected behavior if not used carefully. 4. **Pop all values** * Pros: Simple and efficient way to remove all elements from an array using a while loop. * Cons: Can be slower than other methods due to the need to iterate over the array's elements. **Library usage** None of the benchmark definitions explicitly use any external libraries, but the `splice` method uses the internal implementation details of JavaScript arrays. **Special JS features or syntax** The benchmark defines a special case for deleting an array using the `delete` operator (`"delete arr4;"`). This feature is not specific to modern browsers and may have different behavior in older versions of JavaScript. **Other alternatives** There are other ways to delete an array, such as: * Using `Array.prototype.fill()` with a value of `undefined`: `arr3.fill(undefined)` * Using `Array.prototype.map()` and then spreading the result: `[...arr3].map(() => undefined)` * Using a library like Lodash's `tail` function However, these alternatives are not part of the standard JavaScript specification and may not be supported by all browsers or environments.
Related benchmarks:
remove by splice vs filter array
remove by splice vs filter array v3
lodash remove vs native splice vs shift& pop
Array splice vs delete
Remove by splice vs filter with a known index
Comments
Confirm delete:
Do you really want to delete benchmark?