Home > Web Front-end > JS Tutorial > body text

Comparison of examples of commonly used array traversal methods in JS

php中世界最好的语言
Release: 2018-05-25 13:57:32
Original
994 people have browsed it

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 comparisons

Starting 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 methods

Several 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 comparison

The first type: ordinary for loop

The code is as follows:

for(j = 0; j < arr.length; j++) {
}
Copy after login
Brief description: The simplest one and the most frequently used one. Although the performance is not weak, there is still room for optimization

Second type: optimized version of for loop

The code is as follows:

for(j = 0,len=arr.length; j < len; j++) {
}
Copy after login
Brief description: Use temporary variables to cache the length to avoid repeatedly obtaining the array length. The optimization effect will be more obvious when the array is larger.

This method is basically the highest performance among all loop traversal methods

The third method: weakened version of for loop

The code is as follows :

for(j = 0; arr[j]!=null; j++) {
}
Copy after login
Brief explanation: This method is actually strictly a for loop, but it does not use the length judgment, but uses the variable itself to judge

In fact, the performance of this method is Far smaller than the ordinary for loop

Fourth type: foreach loop

The code is as follows:

arr.forEach(function(e){
});
Copy after login
Brief description: The foreach loop that comes with the array, It is used more frequently, and its performance is actually weaker than the ordinary for loop.

Fifth type: foreach variant

The code is as follows:

Array.prototype.forEach.call(arr,function(el){
});
Copy after login
Brief description: Since foreach comes with the Array type, some non-this type cannot be used directly (such as NodeList), so this variant is created. Using this variant can allow similar arrays to have the foreach function.

The actual performance is weaker than ordinary foreach

Sixth type: forin loop

The code is as follows:

for(j in arr) {
}
Copy after login
Brief description: Many people like to use this loop, but in fact, after analysis and testing, it has the lowest efficiency among the many loop traversal methods

Seventh: map traversal

The code is as follows:

arr.map(function(n){
});
Copy after login
Brief description: This method is also widely used. Although it is more elegant to use, the actual efficiency is not as good as foreach

Eighth type: forof traversal (requires ES6 support)

The code is as follows:

for(let value of arr) {
});
Copy after login
Brief description: This method is used in es6, and its performance is better than forin, but it is still not as good as the ordinary for loop

Performance comparison of various traversal methods

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 screenshot

Analysis 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!