var arr = [12342, 342, 3423, 432, 432, 423, 43, 3, 1, 321, 323, 1, 31, 31, 31, 231, 31, 31];
const narr = [];
for(const item of arr){
narr.push(item)
}
const narr = [];
for(const item in arr){
narr.push(arr[item])
}
const narr = [];
for(let i=0; i<arr.length; i++){
narr.push(arr[i])
}
const narr = [];
arr.forEach( e => narr.push(e));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
for..of | |
for..in | |
for | |
foreach |
Test name | Executions per second |
---|---|
for..of | 6972662.5 Ops/sec |
for..in | 652810.6 Ops/sec |
for | 530400.2 Ops/sec |
foreach | 6908423.0 Ops/sec |
Overview of the Benchmark
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark definition consists of four test cases, each measuring the performance of a different loop construct in JavaScript: for..of
, for simple
(also known as for
without an iterator), for..in
, and foreach
.
Loop Constructs Compared
The four test cases compare the performance of different loop constructs:
for..of
: This loop construct uses a for...of loop, which is a newer syntax that iterates over arrays using the forEach
method.for simple
(or for
): This loop construct uses a traditional for
loop with an index variable.for..in
: This loop construct uses a for...in loop, which iterates over objects using their property names as keys.foreach
: This loop construct uses the forEach
method, which is similar to for..of
, but can be used with arrays or other iterable objects.Pros and Cons of Each Loop Construct
Here's a brief summary of the pros and cons of each loop construct:
for..of
:for simple (or
for)
:for..in
:for..of
, can lead to unexpected behavior if used with arrays.foreach
:for..of
, but can be used with any iterable object.for..of
, can lead to slower performance for large datasets.Library and Special JavaScript Features
None of the test cases use a specific library or special JavaScript feature. However, it's worth noting that forEach
is a built-in method in JavaScript that allows iterating over arrays and other iterable objects.
Test Case Analysis
Each test case measures the execution time of its respective loop construct:
for..of
: This test case uses an array as the input and pushes each element to a new array using the forEach
method.for simple (or
for)
: This test case also uses an array as the input, but with a traditional for
loop instead of for..of
.for..in
: This test case iterates over an object (specifically, the arr
variable) using its property names.foreach
: This test case uses the forEach
method to iterate over an array.Other Alternatives
If you're looking for alternative loop constructs in JavaScript, you may consider:
while
loops: These loops use a conditional statement to control iteration.do...while
loops: These loops are similar to while
loops but execute the body before checking the condition.for..with
loops: These loops are similar to for
loops but allow specifying an initial value for the loop variable.In general, JavaScript's built-in array methods and loops (e.g., forEach
, map
, filter
) provide a concise and efficient way to iterate over data structures like arrays.