Question about converting JavaScript dates to milliseconds: "saving 20% of efficiency and saving 9 characters"

Source: Internet
Author: User

Recently, I was looking at the loose coupling and customizable open-source framework of Baidu, tangram. js, which suddenly focused on a way to get the number of milliseconds:
(+ New Date ())
In fact, there is nothing about this writing method, that is, converting the Date type to the number type using the operator, so I am sure this writing method is not as efficient as the native writing of the Date (new Date (). getTime:
So we did the following test:
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8"/>
<Title> test on converting Date to millisecond </title>
<Style type = "text/css">
<! --
Body {font-size: 12px ;}
Table {border-top: 1px solid # dfdfdf; border-right: 1px solid # dfdfdf ;}
Th, td {padding: 5px; text-align: center ;}
Th {background: #444; color: # fff ;}
Td {border-left: 1px solid # dfdfdf; border-bottom: 1px solid # dfdfdf ;}
// -->
</Style>
</Head>
<Body>
<Script language = "javascript" type = "text/javascript">
// <! [CDATA [
(Function (){
Var bank = function (){};
Var d1, d2, d3, temp;
Var d1 = new Date ();
For (var I = 0; I <1000000; I ++ ){
Temp = new Date (). getTime ();
}
Var d2 = new Date ();
For (var I = 0; I <1000000; I ++ ){
Temp = (+ new Date ());
}
Var d3 = new Date ();
// Print
Document. write ('time used for the first loop: '+ (d2-d1) +' <br/> time used for the first loop: '+ (d3-d2 ));
})();
//]>
</Script>
</Body>
</Html>

The results in different browsers after 1 million cycles are as follows:
IE6:
Time used for the first cycle: 3406
Time used for the first cycle: 5313
IE7:
Time used for the first cycle: 3594
Time used for the first cycle: 5000
IE8:
Time used for the first cycle: 2735
Time used for the first cycle: 3453
Chrome:
Time used for the first cycle: 210
Time used for the first cycle: 337
Opera \ safari \ firefox
The difference is basically 100 ms, but it is still the last slow

Conclusion: It is proved that I am writing the correct + new Date () statement than new Date (). getTime () is inefficient because of type conversion. Generally, the order of magnitude (less than 10 thousand times) is not very large. Therefore, the execution efficiency is almost unnecessary in a browser, therefore, the first writing method is easy to use, saving 9 characters. We recommend that you write the code in the native mode when using js game development and when using an order of magnitude. It can increase the efficiency by 20%.

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.