Comparison of Loop Methods is specifically designed to compare the results of ordinary for loops, foreach, and other traversal methods after a large number of calculations
Currently comparing three major types: for loop, for-in, foreach, and several different uses of for loop
As can be seen, there are significant differences between different methods
Similarly, there is a maximum executable threshold that is currently set, and after exceeding a certain number, the ones that take a long time to execute will be automatically hidden
Among them, for-in will be hidden if it is greater than 100, and map will be hidden if it is greater than 500
It should be noted that for-of can only be used in ES6, so the code is commented out by default
The shortest time-consuming part in the performance test result is marked with green, and the most time-consuming part is marked with pink, which is convenient for readers to observe
Code Premise:
|
||
---|---|---|
Test Code | Performance Analysis | |
Execute code using for loop First method |
| Waiting for execution |
for Loop code execution The second method |
| Waiting for execution |
forLoop code execution The third way |
| Waiting for execution |
for inLoop code execution |
| Waiting for execution |
for each Loop code execution |
| Waiting for execution |
for each Loop code execution,The second method |
| Waiting for execution |
for mapLoop code execution |
| Waiting for execution |
① This tool is excerpted from Dai Lichun's personal blog: https://dailc.github.io/about/about.html
② The tools provided on this site are for reference only. Users should select appropriate methods based on specific scenarios during programming and development.