For of loop javascript9/3/2023 In this article, I described five different loops available in Javascript programming language and tested the performance of three of them. forEach() method is the slowest one in a testing environment. For loop is the second fastest and it seems that. As we can see in the table do…while loop is the fastest one. Let’s now take a look and the summary of all the data we got.Īll loops were tested in Chrome browser. I tested it with similar code to execute, the same amount of executions and in three different browsers. In the article, I tested the performance of three popular loops and one array method, for loop, while loop, do while loop and. In simple words, for.of works with strings and arrays but not with objects.Var string = ‘ Duomly ’ for ( let char of string ) Performance summary.So, for.of loop does not work on objects. Object in JavaScript is not iterable by default. So, for.of loop works on the mentioned object types. String, Array, TypedArray, Map, and Set are all built-in iterables, because each of their prototype objects implements an method. In JavaScript, iterables are objects which can be looped over. I know that's not the explanation you came here for, So let me explain.įor.of loop works only with iterable objects. The for.of statement creates a loop iterating over iterable objects, including: built-in String, Array, array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined iterables. And with this blog I will try to clear the confusion once and for all. The difference between for-of and for-in loop really troubled me when I was learning JavaScript.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |