arr = [1,2,3,4,5]
for (val of arr) Number(val)
arr.forEach(val => Number(val))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
of | |
forEach |
Test name | Executions per second |
---|---|
of | 480849.5 Ops/sec |
forEach | 1457862.5 Ops/sec |
Let's break down the provided benchmarking test cases.
What is being tested?
The test case measures the performance difference between two approaches to iterate over an array and perform an operation on each element: forEach
vs direct iteration using for...of
.
Options compared:
forEach"
: This is a built-in JavaScript method that allows iterating over an iterable object (like an array) while executing a block of code for each element.for...of
loop syntax to iterate over an array, which is a more traditional and explicit way of looping.Pros and Cons:
forEach"
:for...of
.forEach
.Library usage:
There are no libraries used in this benchmarking test case. It only involves standard JavaScript features.
Special JavaScript feature or syntax:
The for...of
loop is a special syntax that allows iterating over arrays and other iterable objects. The .forEach()
method is also a built-in JavaScript method for iterating over an array.
Benchmark preparation code:
The script preparation code arr = [1,2,3,4,5]
initializes an array with five elements for the benchmark.