Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Clear array via array = [] vs array.length = 0
(version: 0)
Comparing performance of:
array = [] vs array.length = 0;
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var list = []; for (var i = 0; i < 1000 * 1000; i++) { list.push(i); }
Tests:
array = []
list = [];
array.length = 0;
list.length = 0;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array = []
array.length = 0;
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 139 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array = []
4112359.5 Ops/sec
array.length = 0;
8648671.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain what's being tested in the provided benchmark. **Benchmark Overview** The benchmark is designed to compare two approaches for clearing an array: using `array = []` or `array.length = 0`. The goal is to determine which approach is faster and more efficient. **Options Compared** There are only two options compared: 1. **Array assignment**: Using `array = []` to create a new empty array. 2. **Array property access**: Using `array.length = 0` to set the length of an existing array to 0, effectively clearing it. **Pros and Cons of Each Approach** **Array Assignment (e.g., `array = []`)** Pros: * Creates a new empty array, which can be more efficient if the original array is large or complex. * Can be faster since it involves creating a new object reference. Cons: * May involve additional memory allocation and deallocation, depending on the JavaScript engine's garbage collection behavior. * Can be slower for very small arrays due to the overhead of creating a new object reference. **Array Property Access (e.g., `array.length = 0`)** Pros: * Faster since it only involves setting a property value without creating a new object reference. * May be more efficient for small arrays, as it avoids the overhead of creating a new array object. Cons: * Modifies an existing array, which can have unexpected side effects if the array is not properly cleared or validated. * Can lead to performance issues if the array is very large or complex, since it may involve rearranging elements or copying data. **Other Considerations** The benchmark likely uses a JavaScript engine that has a relatively fast garbage collector, as evidenced by the high number of executions per second (e.g., 5.5M and 5.3M for each test case). This suggests that the majority of the overhead is due to the array creation process rather than garbage collection. **Library or Framework** The benchmark does not appear to use any external libraries or frameworks, so it's likely a simple implementation of the two approaches being compared. **Special JavaScript Features or Syntax** There are no special JavaScript features or syntax mentioned in the benchmark. The code uses standard JavaScript language features and syntax. **Alternative Approaches** Other possible ways to clear an array might include: * Using `array.splice(0, 0)` to remove all elements from the array. * Using a loop to remove each element individually (e.g., `for (var i = 0; i < array.length; i++) { array[i] = undefined; }`). * Using a third-party library or framework that provides optimized array clearing functions. However, these alternatives are not being tested in this benchmark.
Related benchmarks:
Array .push() vs .unshift(), 1M elements
Array .push() vs .unshift() multiple
empty an array in JavaScript - [] vs setting length
Array.push(x) vs array[n]=x
Comments
Confirm delete:
Do you really want to delete benchmark?