search
HomeWeb Front-endJS TutorialComparison of examples of commonly used array traversal methods in JS

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 Brief description: The simplest one and the most frequently used one. Although the performance is not weak, there is still room for optimization<p style="text-align: left;"></p><p style="text-align: left;">Second type: optimized version of for loop<strong></strong></p>The code is as follows:<p style="text-align: left;"></p><pre class="brush:php;toolbar:false">for(j = 0,len=arr.length; 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. <p style="text-align: left;"></p>This method is basically the highest performance among all loop traversal methods<p style="text-align: left;"></p><p style="text-align: left;">The third method: weakened version of for loop<strong></strong></p>The code is as follows :<p style="text-align: left;"></p><pre class="brush:php;toolbar:false">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 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!

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
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool