JavaScript appendchild,innerhtml,join Performance Comparison code _javascript tips

Source: Internet
Author: User
Tags javascript appendchild
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title>appendChild,innerHTML,join</title>
<script type= "Text/javascript" ><!--
To add a span label using the AppendChild () method
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
}
To add a span label using the innerHTML () method
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 with 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>
<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>
Related Article

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.