The Navigation Timing API provides data that can be used to measure the performance of a web site. Unlike JavaScript-based libraries that have historically been used to collect similar information, the Navigation Timing API can be much more accurate and reliable.
This article currently describes Navigation Timing Level 1. There is a specification for Level 2, but it is not yet covered here.
You can use the Navigation Timing API to gather performance data on
the client side, which you can then transmit to a server using
or other techniques.XMLHttpRequest
This API lets you measure data that was previously difficult to obtain, such as the amount of time needed to unload the previous page, how long domain lookups take, the total time spent executing the window's
handler, and so forth.load
The
property returns a Performance object. While this interface is defined by the High Resolution Time API, the Navigation Timing API adds two properties: window.performance
and timing
, of the types below.navigation
Provides methods and properties to store and retrieve metrics regarding the browser's document navigation events. For example, this interface can be used to determine how much time it takes to load or unload a document.
Used as the type for the value of
, objects of this type contain timing information that can provide insight into web page performance.timing
The type used to return the value of
, which contains information explaining the context of the load operation described by this navigation
Performance
instance.window.onload
total time, etc.Calculate the total page load time
To compute the total amount of time it took to load the page, you can use the following code:
const perfData = window.performance.timing;
const pageLoadTime = perfData.loadEventEnd - perfData.navigationStart;
This subtracts the time at which navigation began (
) from the time at which the load event handler returns (navigationStart
). This gives you the perceived page load time.loadEventEnd
Calculate request response time
You can calculate the time elapsed between the beginning of a request and the completion of getting the response using code like this:
const connectTime = perfData.responseEnd - perfData.requestStart;
Here, the time at which the request was initiated (
). from the time at which the response was finished being received (requestStart
).responseEnd
Calculate page render time
As another example of an interesting piece of data you can obtain using the Navigation Timing API that you can't otherwise easily get, you can get the amount of time it took to render the page:
const renderTime = perfData.domComplete - perfData.domLoading;
This is obtained by starting with the time at which loading of the DOM and its dependencies is complete (
) and subtracting from it the time at which parsing of the DOM began (domComplete
).domLoading
Window.performance.timing