Recently, I was looking at tangram.js, Baidu’s loosely coupled and customizable open source framework, and my eyes suddenly focused on a way to get milliseconds:
( new Date())
In fact, this way of writing is nothing but conversion using operators. The date is of type number, so I am sure that this way of writing is not as efficient as the native way of writing dates (new Date().getTime()):
So I did the following test:
< meta charset="utf-8" />
Test on converting Date into milliseconds
Loop one million times respectively The results in different browsers are as follows:
IE6:
Time spent in the first cycle: 3406
Time spent in the first cycle: 5313
IE7:
Time spent in the first cycle :3594
First cycle time: 5000
IE8:
First cycle time: 2735
First cycle time: 3453
chrome:
First Time spent in the first loop: 210
Time spent in the first loop: 337
operasafarifirefox
Basically a difference of 100ms, but still the last slow one
Conclusion: Prove that I am right new The writing method of Date() is less efficient than new Date().getTime() because of the type conversion. Usually the order of magnitude we commonly use (within 10,000 times) is not very large, so there is almost no need to consider the issue of execution efficiency in a browser. So the first way of writing is better and saves 9 characters. When using js game development, when a large order of magnitude is used, native writing is recommended. Can improve efficiency by 20%.