This time I will bring you a comparison of examples of commonly used array traversal methods in JS. What are the precautions for commonly used JS array traversal methods? The following is a practical case, let’s take a look.
Preface
This article belongs to the same series as the previous article on JS variable exchange methods and performance analysis comparison. This article continues the analysis. Several commonly used array traversal methods in JS and their respective performance comparisonsStarting from
Last time I analyzed several commonly used variables in JS After exchanging the methods and their respective performances, I felt that this method was quite good, so I extracted the core logic, encapsulated it into a template, and planned to expand it into a series. This article is the second article in the series, an analysis and comparison of JS array traversal methodsSeveral ways of JS array traversal
JS array traversal is basically for, forin, foreach, forof, map, etc. The following introduces several array traversal methods used in the analysis of this article and performance analysis and comparisonThe first type: ordinary for loop
The code is as follows:for(j = 0; j < arr.length; j++) { }
Second type: optimized version of for loop
The code is as follows:for(j = 0,len=arr.length; j < len; j++) { }
The third method: weakened version of for loop
The code is as follows :for(j = 0; arr[j]!=null; j++) { }
Fourth type: foreach loop
The code is as follows:arr.forEach(function(e){ });
Fifth type: foreach variant
The code is as follows:Array.prototype.forEach.call(arr,function(el){ });
Sixth type: forin loop
The code is as follows:for(j in arr) { }
Seventh: map traversal
The code is as follows:
arr.map(function(n){ });
Eighth type: forof traversal (requires ES6 support)
The code is as follows:
for(let value of arr) { });
The above listed several methods all have one Once we have done a comparative analysis, we can basically conclude that:
Ordinary for loop is the most elegant
(PS: All the above codes are just Perform an empty loop, there is no internal execution code in the loop, just analyze the time of each loop)
Performance comparison screenshotAnalysis results 1
The data in the screenshot below is the conclusion drawn after running it 100 times in chrome (supports es6) (run 10 times each time, 10 cycles in total, and get Analysis results)
It can be seen that the forin loop is the slowest. The optimized ordinary for loop is the fastest
Analysis results 2
The following screenshot data is the conclusion drawn after running 1000 times in chrome (supports es6) (Run 100 times each time, 10 cycles in total, and get the analysis results)
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to php Chinese Other related articles online!
Recommended reading
How to use JS to merge multiple arrays for recalculation
Detailed explanation of the steps to use antd drop-down box linkage
The above is the detailed content of Comparison of examples of commonly used array traversal methods in JS. For more information, please follow other related articles on the PHP Chinese website!