Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
empty an array in JavaScript - splice vs setting length. 444 333
(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:
3 years ago
by:
Guest
Jump to the latest result
Tests:
Set length to zero
var size = 1000; var arr2 = []; for (var i = 0; i < size; i++){ arr2.push(i); } arr2.length = 64;
Splice
var size = 1000; var arr3 = []; for (var i = 0; i < size; i++){ arr3.push(i); } arr3.splice(64);
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:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set length to zero
193718.7 Ops/sec
Splice
161934.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Overview** The benchmark is designed to compare two approaches for emptying an array in JavaScript: setting the length of the array to zero, and using the `splice()` method with a specific index. The test aims to determine which approach is faster. **Test Case 1: Setting Length to Zero** This test case creates an array of size 1000, populates it with integers from 0 to 999, and then sets the length of the array to 64. ```javascript var size = 1000; var arr2 = []; for (var i = 0; i < size; i++) { arr2.push(i); } arr2.length = 64; ``` **Pros and Cons** Setting the `length` property directly has the following advantages: * It is a simple and straightforward way to reset an array. * It avoids creating a new array or modifying existing elements. However, it also has some potential drawbacks: * If the array contains sparse properties (i.e., some indices are missing), setting the length may not work as expected. * Some browsers may have issues with this approach. On the other hand, using `splice()` has its own set of advantages and disadvantages: * It is generally considered a more modern and safer way to remove elements from an array. * It does not modify existing elements or create new arrays in memory. However, it also has some potential drawbacks: * It can be slower than setting the length property directly, especially for large arrays. * It requires knowing the specific index at which to start removing elements. **Test Case 2: Using Splice()** This test case creates an array of size 1000, populates it with integers from 0 to 999, and then uses `splice()` to remove elements starting from index 64. ```javascript var size = 1000; var arr3 = []; for (var i = 0; i < size; i++) { arr3.push(i); } arr3.splice(64); ``` **Library: None** Neither of the test cases uses a specific JavaScript library. The only external dependency is the browser's implementation of the `length` property and the `splice()` method. **Special JS Feature or Syntax: None** There are no special JavaScript features or syntax used in these test cases. **Benchmark Results** The benchmark results show that setting the length to zero is faster than using `splice()`. The exact numbers vary depending on the browser and execution per second, but overall, setting the length is slightly faster. **Alternatives** Other approaches to emptying an array in JavaScript include: * Using `forEach()` with a callback function that returns `undefined` or `null` * Using `map()` with an empty callback function * Using `filter()` with an empty callback function These alternatives may have different performance characteristics and may be more suitable for specific use cases. However, they are not typically considered as fast as setting the length property directly or using `splice()`.
Related benchmarks:
Slice vs splice 2 ...
empty an array in JavaScript - splice vs setting length faster
Array splice vs delete
Array.splice(0, N) vs Array.length === N
Empty array: Splice vs Shift
Comments
Confirm delete:
Do you really want to delete benchmark?