JavaScript loop speed for loop | | While loop

Source: Internet
Author: User

JavaScript as the scripting language for the client, generally speaking, is the speed of the first. The speed required depends on what makes the fastest.

The circulation basically all language all has, is also the most commonly used, its embodiment, basically can represent the speed the embodiment.

Basically 2 loops in javascript: For loop while loop

The For loop is divided into 3 kinds: for ++;for--;for in

While loops are divided into 2 kinds (do-while statements are not considered): while ++;while--

Which one is faster?

Casually write a few test statements, basically can compare the fastest cycle is which.

Come to the conclusion: for-and while-should be the fastest, the speed of the two are similar, after many experiments, or can not compare which faster, and know the details of the implementation of the inside please contact me, thank you.

For in the slowest, always think the simplest of the fastest, after trying to know .... For in is too slow to be sad.

Step 1:for Loop: < script type = "Text/javascript" >
/**/ /**
*test for + + loop;for--loop;for in loop
*which one is the fastest one
*
*by Sun 20071211
*/
var arrlength = window.prompt ("Input a number for loop count", 100000);

document.write ("Loop count:" + arrlength + "<br/>");

var Temparr = new Array ();
var Tempvalue;
var arrendposition = arrLength-1;

Timer
var starttime;
var Endtime;

Fill
for (var i = arrendposition i >= 0; I-) ... {
Temparr.push (i);
}

For + + loop
StartTime = new Date (). GetTime ();
for (var i = 0; i < arrlength i + +) ... {
Tempvalue = Temparr[i];
}
Endtime = new Date (). GetTime ();
document.write ("for + + Time:" + (Endtime-starttime));

document.write ("<br/>");

For--loop
StartTime = new Date (). GetTime ();
for (var i = arrendposition i >= 0; I-) ... {
Tempvalue = Temparr[i];
}
Endtime = new Date (). GetTime ();
document.write ("For--time:" + (Endtime-starttime));

document.write ("<br/>");

For In loop
StartTime = new Date (). GetTime ();
for (var i in Temparr) ... {
Tempvalue = Temparr[i];
}
Endtime = new Date (). GetTime ();
document.write ("For in Time:" + (Endtime-starttime));
</script >

Test result speed: for-< for + + < for in (slowest)

  Step 2:while loop: < Script type = "Text/javascript" >
/**//**
 *test while  ; ++ loop;while -- loop
 *
 *by sun 20071211
 */
  var  arrLength  =  window.prompt ("Input a number for loop count", 100000);

 document.write ("loop count : " + arrlength + <br/>);  

  var &n bsp;temparr  =   new  array ();
  var  tempValue;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.