var arr = [
{ "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" },
{ "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" },
{ "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" },
{ "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" },
{ "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" },
{ "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" },
{ "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" },
{ "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" },
{ "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" },
{ "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" },
{ "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" },
{ "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" },
{ "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" },
{ "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" },
{ "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" },
{ "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" },
{ "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" },
{ "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" },
{ "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" },
{ "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" }, { "key": "value" },
]
var newarr1 = []
var newarr2 = []
for (var i = 0; i < arr.length; i++) {
newarr1.push(arr[i])
}
for (var i = 0; i < arr.length; i++) {
const el = arr[i]
newarr2.push(el)
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Direct Array Access | |
Local Variable Access |
Test name | Executions per second |
---|---|
Direct Array Access | 311247.3 Ops/sec |
Local Variable Access | 294887.1 Ops/sec |
Let's break down the benchmark and its components.
Benchmark Definition
The benchmark compares two approaches to access elements in an array:
arr
array directly, without introducing any intermediate variables. In this case, i
is the index variable, and arr[i]
accesses the element at index i
.el
to hold the current element being processed. The code then uses newarr2.push(el)
to push the element onto the newarr2
array.Options Being Compared
The benchmark is comparing the performance of two options:
arr
array directly, without introducing any intermediate variables.el
to hold the current element being processed.Pros and Cons of Each Approach
Library and Special JS Features
There are no libraries mentioned in this benchmark. However, it's worth noting that JavaScript engines like Firefox 121 use various optimizations and features, such as:
These features can affect the performance of the benchmark results.
Test Case Special JS Feature
There is no special JavaScript feature or syntax used in this benchmark. The code uses standard JavaScript features, such as:
for
loops)arr[i]
)push()
)Overall, this benchmark provides a simple and straightforward comparison between two approaches to accessing elements in an array. It's a useful tool for developers looking to optimize their code and understand the trade-offs between different approaches.