Javascript appendChild, innerHTML, and join performance comparison code

Source: Internet
Author: User
Tags javascript appendchild

Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> appendChild, innerHTML, join </title>
<Script type = "text/javascript"> <! --
// Use appendChild () to add a span tag
Function AppendDiv ()
{
Var times = parseInt (document. getElementById ('tbtimes '). value );
Var oDiv = document. getElementById ('div1 ');
Var Text = document. getElementById ('tbtext'). value;
Var Time = new Date ();
Var StartTime = Time. getTime ();
For (var I = 1; I <times; I ++)
{
Var span = document. createElement ('span ');
Span. appendChild (document. createTextNode (Text ));
ODiv. appendChild (span );
}
Var Time1 = new Date ();
Var EndTime = Time1.getTime ();
Document. getElementById ('tbdate'). value = EndTime-StartTime
}
// Use the innerhtml () method to add a span tag
Function InnerHTML ()
{
Var times = parseInt (document. getElementById ('tbtimes '). value );
Var oDiv = document. getElementById ('div1 ');
Var Text = document. getElementById ('tbtext'). value;
Var Time = new Date ();
Var StartTime = Time. getTime ();
For (var I = 1; I <times; I ++)
{
ODiv. innerHTML + = "<span>" + Text + "</span> ";
}
Var Time1 = new Date ();
Var EndTime = Time1.getTime ();
Document. getElementById ('tbdate'). value = EndTime-StartTime
}
// Add span using push in Array
Function Join ()
{
Var times = parseInt (document. getElementById ('tbtimes '). value );
Var oDiv = document. getElementById ('div1 ');
Var Text = document. getElementById ('tbtext'). value;
Var Time = new Date ();
Var StartTime = Time. getTime ();
Var MyArray = new Array ();
For (var I = 1; I <times; I ++)
{
MyArray. push ("<span>" + Text + "</span> ");
}
ODiv. innerHTML = MyArray. join ('');
Var Time1 = new Date ();
Var EndTime = Time1.getTime ();
Document. getElementById ('tbdate'). value = EndTime-StartTime
}

// --> </Script>
</Head>
<Body>
<Input type = "text" id = "tbText" value = "ws_hgo"/> <br/>
<Input type = "text" id = "tbTimes" value = "1000"/> <br/>
<Input type = "text" id = "TbDate"/> <br/>
<Input id = "Button1" type = "button" value = "Append" onclick = "AppendDiv ();"/>
<Input id = "Button2" type = "button" value = "innerHTML" onclick = "InnerHTML ();"/>
<Input id = "Button3" type = "button" value = "Join" onclick = "Join ();"/>
<Div id = "div1">
</Div>
</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.