Home > Web Front-end > JS Tutorial > Regarding the conversion of js date into milliseconds 'saving 20% ​​efficiency and saving 9 characters' problem_time and date

Regarding the conversion of js date into milliseconds 'saving 20% ​​efficiency and saving 9 characters' problem_time and date

WBOY
Release: 2016-05-16 17:55:44
Original
862 people have browsed it

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:

Copy code The code is as follows:




< 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%.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template