Comparison of for and each performance and foreach Performance

Source: Internet
Author: User

Comparison of for and each performance and foreach Performance

<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> for and each performance comparison </title>
</Head>
<Body>
<Div id = "test"> </div>
<Input type = "button" value = "for" onclick = "getFor ();"/>
<Input type = "button" value = "each" onclick = "getEach ();"/>
<Script type = "text/javascript" src = "http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"> </script>
<Script type = "text/javascript" language = "javascript">
$ (Function (){
// Add data
For (var I = 0; I <200; I ++ ){
$ ("# Test"). append ('<div class = "test" title = ""> test </div> ');
}

});
Function getFor (){
Var getclass = $ (". test ");
Var length = getclass. length;
Console. log (length );
Var timestart = new Date (). getTime ();
For (var I = 0; I <length; I ++ ){
Getclass [I]. title = 'I am ';
}
Var timeend = new Date (). getTime ();
Alert ("for time consumption:" + (timeend-timestart ));
}
Function getEach (){
Var timestart = new Date (). getTime ();
$ (". Test"). each (function (){
$ (This). attr ('title', 'mybackback ');
});
Var timeend = new Date (). getTime ();
Alert ("each time consumed:" + (timeend-timestart ));

}
</Script>

</Body>
</Html>

 

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.