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%.