let size = 10000
let arr = []
for (let i = 0; i < size; i++){
arr.push(i)
}
let sum = 0
for (let i = arr.length; --i >= 0; ) {
sum += arr[i]
}
arr.length = 0
let size = 10000
let arr = []
for (let i = 0; i < size; i++){
arr.push(i)
}
let sum = 0
for (let v; (v = arr.pop()); ) {
sum += v
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array.length = 0 | |
array.pop() |
Test name | Executions per second |
---|---|
array.length = 0 | 9707.1 Ops/sec |
array.pop() | 8728.9 Ops/sec |
I'll break down the benchmark and explain what's being tested.
Benchmark Overview
The MeasureThat.net website allows users to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark definition and two individual test cases.
What is Tested?
The benchmark tests two different approaches for iterating over an array in reverse order, clearing the array, and calculating a sum:
Approach 1: Using arr.length
("array.length = 0"
test case)
for
loop.for
loop, summing up the values.arr.length
property is set to 0.Approach 2: Using Array.pop()
("array.pop()"
test case)
for
loop.arr.pop()
, summing up the values.Options Compared
The two approaches are compared to determine which one performs better in terms of execution speed.
Pros and Cons of Each Approach:
arr.length
arr.length
).Array.pop()
Library Used:
None explicitly mentioned in the provided benchmark definition. However, it's likely that MeasureThat.net uses a JavaScript engine or runtime environment (e.g., V8) that provides a standardized way to execute and measure JavaScript code.
Special JS Feature/Syntax:
The for...of
loop is not used in either test case. It would require an alternative approach if you wanted to use it for iterating over the array elements.