var list = new Array(10*1000);
for (var i = 0; i < 10 * 1000; i++) {
list[i]= i * Math.random();
}
while(list.length) {
list = list.slice(1)
}
while(list.length) {
list.shift()
}
while(list.length) {
list.splice(0,1)
}
let i = 0
let ln= list.length
while (i < ln) {
list[i];
delete list[i];
i++
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
slice | |
shift | |
splice | |
delete |
Test name | Executions per second |
---|---|
slice | 66908532.0 Ops/sec |
shift | 231062272.0 Ops/sec |
splice | 224277136.0 Ops/sec |
delete | 82615032.0 Ops/sec |
Let's break down the benchmark and its results.
What is being tested?
The provided JSON represents a JavaScript microbenchmark that compares the performance of four different approaches to modify an array:
slice()
: Creates a new array with all elements from the original array, excluding the first element.shift()
: Removes the first element from the original array and returns it.splice(0, 1)
: Removes the first element from the original array and returns an array with the removed element.delete
statement: Deletes the first occurrence of a property in the object (not directly applicable to arrays, but we'll assume it's used as a stand-in for removing an element).Options being compared
The benchmark compares the performance of these four approaches on a large array of 10,000 elements, populated with random numbers.
Pros and cons of each approach:
slice()
: Creates a new array, which can be memory-intensive. However, it's generally a fast operation.shift()
: Removes an element from the original array, making subsequent operations faster. However, it requires more cache misses compared to splice
.splice(0, 1)
: Similar to shift()
, but creates an additional array object. It's also relatively fast but can be slower due to the extra object creation.delete
statement: Not directly applicable to arrays, but used as a stand-in for removal. This approach is likely to be slow due to its nature.Library and purpose
None of the approaches explicitly use a library or a framework, but they do utilize JavaScript's built-in array methods. The splice()
method, in particular, uses an internal implementation that modifies the original array's length property.
Special JS features or syntax
There are no specific special features or syntax used in this benchmark beyond what's standard in modern JavaScript.
Other considerations
The benchmark measures executions per second (FPS), which can be influenced by factors like:
Alternative approaches
Other methods to modify an array could be explored in similar benchmarks, such as:
Array.prototype.map()
and then reducing the resulting arrayArray.prototype.filter()
and then concatenating or using join()
In general, when optimizing array operations, consider factors like memory allocation, cache performance, and garbage collection to understand why certain approaches might be faster than others.
For more detailed analysis of specific benchmarks or microbenchmarks, I'd recommend exploring:
Keep in mind that benchmarking is an area of ongoing research and optimization, and the best approach often depends on the specific use case, performance requirements, and target audience.