About JS date converted to milliseconds "save 20% efficiency and save 9 Characters" problem _ Time Date

Source: Internet
Author: User
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%.

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.