Core points
- High Resolution Time API is a JavaScript interface that provides current time with submillisecond resolution, not affected by system clock deviation or adjustment, and provides more reliable and accurate performance measurement options than traditional
Date.now()
methods. . - The high-resolution time API is not widely supported by all browsers, mainly supporting Internet Explorer 10, Firefox 15 (unprefixed), and Chrome 20 (with the "webkit" prefix). It is recommended to use functions to test browser support and prefix usage.
- Developers can easily integrate high-resolution time APIs into their code by replacing
Date.now()
withperformance.now()
, thus improving measurement accuracy without major changes to the code structure.
In today's world, performance is crucial. Developers need to be able to accurately measure the performance of their software. For those engaged in web development, W3C provides a brand new API to reliably time count, which is the high resolution time API. This article will explore the high-resolution time API and show how to use it. To measure the performance of functions, web developers used to use the JavaScript method. Usually, the timing code looks like this:
Date.now()
var startTime = Date.now(); // 一个耗时的函数 foo(); var test1 = Date.now(); // 另一个耗时的函数 bar(); var test2 = Date.now(); // 打印结果 console.debug("Test1 time: " + (test1 - startTime)); console.debug("Test2 time: " + (test2 - test1));A JavaScript interface that provides the current time of submillisecond resolution and is not affected by system clock deviation or adjustments
". On October 23, 2012, the specification became the W3C recommendation recommendation—the last step before it became the recommendation. On December 17, it became the W3C recommendation (updated on December 17). Date.now()
How to work with high resolution time API
I must admit that this is the easiest API I have ever read because it contains only one method. This API extends the Performance interface, which is also used by the Navigation Timing API. If you've never heard of it, check out the Navigation Timing API: How to analyze page loading effectively. The only way to be exposed is now()
, which returns a DOMHighResTimeStamp
indicating the current time in milliseconds. The timestamp is very accurate, with an accuracy of one thousandth of a millisecond. Note that although Date.now()
returns the number of milliseconds elapsed since 00:00:00 UTC on January 1, 1970, performance.now()
returns the number of milliseconds invoked from performance.timing.navigationStart()
(the document navigation starts) to performance.now()
, The fractional part is microseconds. Another important difference between Date.now()
and performance.now()
is that the latter is monotonically increasing, so the difference between the two calls will never be negative. Maybe you're wondering how the high resolution time API will change your code. The good news is that it won't change anything. You can improve the accuracy of your measurements by simply replacing Date.now()
with performance.now()
. With this in mind, the previous code will be rewritten as follows:
var startTime = Date.now(); // 一个耗时的函数 foo(); var test1 = Date.now(); // 另一个耗时的函数 bar(); var test2 = Date.now(); // 打印结果 console.debug("Test1 time: " + (test1 - startTime)); console.debug("Test2 time: " + (test2 - test1));
Compatibility
Currently, only a few browsers support high-resolution time APIs. The only desktop browsers that support this API are Internet Explorer 10, Firefox 15 (unprefixed), and Chrome 20 (with "webkit" prefix, performance.webkitNow()
). Chrome seems to be using the unprefixed version starting with version 24. At the time of writing, no mobile browser supports this API. Since the support range is not wide, a function is first needed to test the browser support situation and whether it is prefixed. The following function will return an empty string when the browser uses the unprefixed version of the API. If a prefixed version is used, the prefix is returned. If the API is not supported, return null.
var startTime = performance.now(); // 一个耗时的函数 foo(); var test1 = performance.now(); // 另一个耗时的函数 bar(); var test2 = performance.now(); // 打印更精确的结果 console.debug("Test1 time: " + (test1 - startTime)); console.debug("Test2 time: " + (test2 - test1));
For browsers that do not support this API, you can use a shim. Shim's author Tony Gentilcore is one of the contributors to the API. In his post titled "A better timer for JavaScript", Gentilcore wrote code that first searches for native support and then uses the Date.getTime()
method as a fallback. The code looks like this:
function getPrefix() { var prefix = null; if (window.performance !== undefined) { if (window.performance.now !== undefined) prefix = ""; else { var browserPrefixes = ["webkit","moz","ms","o"]; // 测试所有厂商前缀 for(var i = 0; i < browserPrefixes.length; i++) { if (window.performance[browserPrefixes[i] + "Now"] != undefined) { prefix = browserPrefixes[i]; break; } } } } return prefix; }
Integrate all content
This section will guide you through a simple demo page. The demo will first test the browser support, and then use a function called doBenchmark
which relies on two virtual functions to benchmark using the performance.now()
method. Note that I introduced an API-independent getTime()
function. Its sole purpose is to avoid unnecessary duplication and make the code more concise. The source code for the demonstration is as follows: (The lengthy sample code is omitted here because it does not match the question requirements and is too long)
Conclusion
In this article, I've covered what the high resolution time API is and how to use it. As I mentioned, it hasn't been widely supported yet, so to accurately test your web application you'll have to wait for a while. However, as you can see, the API is very simple because it contains only one method. Therefore, once browser support is improved, migration to high resolution time will be quick and easy.
(The FAQs part is omitted here because it does not match the question requirements and is too long)
The above is the detailed content of Discovering the High Resolution Time API. For more information, please follow other related articles on the PHP Chinese website!

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

Enhance Your Code Presentation: 10 Syntax Highlighters for Developers Sharing code snippets on your website or blog is a common practice for developers. Choosing the right syntax highlighter can significantly improve readability and visual appeal. T

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

This article presents a curated selection of over 10 tutorials on JavaScript and jQuery Model-View-Controller (MVC) frameworks, perfect for boosting your web development skills in the new year. These tutorials cover a range of topics, from foundatio

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor
