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 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 judgeIn 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 loopPerformance 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 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!

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

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

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

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

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

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

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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 latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

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
The latest (2018.2.1) professional PHP integrated development tool
