Recently looking at loosely coupled customizable Baidu's open source framework tangram.js eyes suddenly focus on a way to get the number of milliseconds:
(+new Date ())
In fact, this kind of writing is not the same as the operator conversion date number type, so I am sure that this type of writing does not have the original date (new Date (). GetTime ()) High efficiency:
So I did the following tests:
Copy Code code as follows:
<! DOCTYPE html>
<meta charset= "Utf-8"/>
<title> testing of the conversion of date into milliseconds </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>
<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 (' First cycle time: ' + (D2-D1) + ' <br/> time of First cycle: ' + (D3-D2) ');
})();
]]>
</script>
</body>
The results of the 1 million-times separate loops in different browsers are as follows:
IE6:
First cycle time: 3406
First cycle time: 5313
IE7:
First cycle time: 3594
First cycle time: 5000
IE8:
First cycle time: 2735
First cycle time: 3453
Chrome:
First Cycle time: 210
First Cycle time: 337
Opera\safari\firefox
Basically a difference of 100ms, but still the last kind of slow
Conclusion: To prove that I am correct +new date () is less efficient than the new date () GetTime (), the reason for the type conversion, usually we often use the order of magnitude (10,000) is not very large, so in a browser almost do not consider the problem of implementation efficiency, So the first way to use the wording is good, but also save 9 characters. When the use of JS game development, when the use of large orders of magnitude, the proposed native writing. Can increase the efficiency of 20%.