In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "what are the loop statements and process control statements in JavaScript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Loop statement
As we all know, the commonly used loop statements are for, while, do-while and for-in,forEach. In addition to the slightly lower performance of for-in and forEach, our choices of the first three are more based on requirements than performance considerations. Today, we will test their respective performance and tell us what optimizations we can do in the most extreme cases.
First, let's talk about why for-in and forEach are slower than the others. For-in is generally used for traversing object property names, because each iterative operation will search the properties of the instance itself and the properties on the prototype chain at the same time, so it must be inefficient; while forEach is a function-based iteration (it should be noted that all versions of ie are not supported, libraries such as JQuery can be used if necessary), the cost of calling external methods on each array item is the main reason for the slow speed.
Then let's take a look at what for, while, and do-while do in each iteration.
Var length = items.length; for (var I = 0; I < items [I]) process (items [I]); var j = 0; while (j < length) process (items [j + +]); var k = 0; do {process (items [k + +]);} while (k < length)
In each of the above loops, this occurs each time the loop body is run:
Comparison of numerical values in primary control conditions (I < length)
Comparison of whether the result of a control condition is true (I < length = true)
A self-increment operation (iTunes +)
One-time array search (items [I])
A function call to process (items [I])
We can improve loop performance by reversing the order of arrays:
For (var I = items.length; items [I]) process (items [I]); var j = items.length; while (Jmurm -) process (items [j]); var k = items.length-1; do {process (items [k]);} while (items -)
In this example, a reverse loop is used and the subtraction operation is integrated into the loop condition. Now each control condition is simply compared to zero. Comparing the control condition with the true value, any non-zero number is automatically converted to true, and the zero value is equivalent to false. In fact, the control conditions are compared from two (is the number of iterations less than the total? Is it true? Reduced to one comparison (is it true? ). Each iteration is reduced from two comparisons to one, which further improves the cycle speed.
Performance testing:
So is that really the case? Real gold is not afraid of browsing machine testing. The test code is very simple, encapsulating 8 functions for 8 different situations (profiles information cannot be printed without timer firefox, unknown reason):
If (value = = 0) {
Return result0
} else if (value = = 1) {
Return result1
} else if (value = = 2) {
Return result2
} else if (value = = 3) {
Return result3
} else if (value = = 4) {
Return result4
} else if (value = = 5) {
Return result5
} else if (value = = 6) {
Return result6
} else if (value = = 7) {
Return result7
} else if (value = = 8) {
Return result8
} else if (value = = 9) {
Return result9
} else {
Return result10
}
When the array length is 100, we find that the results under firefox are indeed similar to those expected: for-each and for-in are inefficient, and reverse order is slightly more efficient than positive order. (profiles under chrome does not display because the time is too short)
When the amount of data reaches 100w, the results under firefox and chrome are as expected, but they are also slightly different. For-in under ff performed better than for- each, while for-in under chrome performed badly, giving a warning directly. Although the performance of reverse iteration is slightly improved, it does not improve much, and reduces the readability of the code.
This is the end of the content of "what are the loop statements and flow control statements in JavaScript"? thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.