Home  >  Article  >  Web Front-end  >  JS summary of array traversal methods and performance comparison

JS summary of array traversal methods and performance comparison

php中世界最好的语言
php中世界最好的语言Original
2018-06-08 14:51:141330browse

This time I will bring you a summary and performance comparison of array traversal methods using JS. What are the precautions for a summary and performance comparison of array traversal methods using JS? 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++) {
}

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++) {
}

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++) {
}

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){
});

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){
});

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) {
}

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){
});

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) {
});

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 methodsThe 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) (100 times each time, 10 cycles in total) , the obtained analysis results)

In addition, you can use the analysis tools in the following demo to analyze and compare the JS array traversal method

I believe you have read the case in this article You have mastered the method. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

vue-simplemde to drag and paste images

actual case of react props and state attributes Detailed explanation

The above is the detailed content of JS summary of array traversal methods and performance comparison. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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