Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
empty an array in JavaScript - splice vs setting length faster
(version: 0)
Based on question on http://stackoverflow.com/questions/1232040/how-do-i-empty-an-array-in-javascript
Comparing performance of:
Set length to zero vs Splice
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var size = 10000; var arr = []; for (var i = 0; i < size; i++){ arr.push(i); }
Tests:
Set length to zero
arr.length = 0;
Splice
arr.splice(0, arr.length);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set length to zero
Splice
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set length to zero
23012442.0 Ops/sec
Splice
30456768.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark is testing two different approaches to empty an array in JavaScript: 1. `arr.length = 0;` 2. `arr.splice(0, arr.length);` **Options Compared** The two options are compared in terms of performance, specifically execution speed. * Option 1: Setting the length of the array to 0 using `arr.length = 0;`. This is a simple and direct way to set the length of an array. * Option 2: Using `splice(0, arr.length);` to remove all elements from the beginning of the array. This method also sets the length of the array but uses a more efficient algorithm. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Option 1 (arr.length = 0;)**: + Pros: Simple, straightforward, and easy to implement. + Cons: May not be as efficient for large arrays, as it involves a separate operation to update the array length. * **Option 2 (splice(0, arr.length);)**: + Pros: More efficient than setting the length directly, especially for large arrays. It also uses less memory, as it only updates the length of the array without creating a new one. + Cons: May be slightly more complex to implement, and some older browsers may not support `splice` or have performance issues with it. **Library Usage** There is no explicit library usage in this benchmark. However, if you're using a JavaScript environment that doesn't support `splice`, you might need to use a polyfill or alternative implementation. **Special JS Feature/Syntax** This benchmark doesn't use any special JavaScript features or syntax beyond what's required for the comparison (i.e., `splice`). If it did, I'd be happy to explain! **Other Alternatives** If you're interested in exploring other approaches, here are some alternatives: * Using a custom loop to iterate over the array and remove elements: `for (var i = 0; i < arr.length; i++) { arr.splice(i, 1); }` * Using `fill()` method with an empty value: `arr.fill(undefined)` * Using `map()` function and then `set()`: `arr.map(() => undefined).forEach((_, index) => set(arr[index], null))` Keep in mind that these alternatives might have different performance characteristics or require additional setup. I hope this explanation helps you understand the benchmark better!
Related benchmarks:
empty an array in JavaScript - splice vs setting length
empty an array in JavaScript - splice vs setting length 2
empty an array in JavaScript - splice vs setting length. 444
Array.splice(0, N) vs Array.length === N
Comments
Confirm delete:
Do you really want to delete benchmark?