The Efficiency API measures the responsiveness of your reside internet software on genuine consumer units and community connections. It will probably assist establish bottlenecks on your client-side and server-side code with:

  • consumer timing: Customized size of client-side JavaScript serve as efficiency
  • paint timing: Browser rendering metrics
  • useful resource timing: Loading efficiency of belongings and Ajax calls
  • navigation timing: Web page loading metrics, together with redirects, DNS look-ups, DOM readiness, and extra

The API addresses a number of issues related to conventional efficiency overview:

  1. Builders steadily check programs on high-end PCs hooked up to a quick community. DevTools can emulate slower units, however it gained’t at all times spotlight real-world problems when the vast majority of shoppers are working a two year-old cellular hooked up to airport WiFi.
  2. 3rd-party choices corresponding to Google Analytics are steadily blocked, resulting in skewed effects and assumptions. You may additionally stumble upon privateness implications in some nations.
  3. The Efficiency API can appropriately gauge quite a lot of metrics higher than strategies corresponding to Date().

Wish to be told extra about the use of the Efficiency API? 👀 Get started right here… 🚀Click on to Tweet
The next sections describe techniques you’ll be able to use the Efficiency API. Some wisdom of JavaScript and web page loading metrics is really useful.

Efficiency API Availability

Most present browsers make stronger the Efficiency API – together with IE10 and IE11 (even IE9 has restricted make stronger). You’ll stumble on the API’s presence the use of:

if ('efficiency' in window) {
  // use Efficiency API

It’s now not imaginable to totally Polyfill the API, so be cautious about lacking browsers. If 90% of your customers are luckily surfing with Web Explorer 8, you’d simplest be measuring 10% of shoppers with extra succesful programs.

The API can be utilized in Internet Staff, which offer a strategy to execute advanced calculations in a background thread with out halting browser operations.

Maximum API strategies can be utilized in server-side Node.js with the usual perf_hooks module:

// Node.js efficiency
import { efficiency } from 'node:perf_hooks';
// or in Not unusual JS: const { efficiency } = require('node:perf_hooks');

console.log( );

Deno supplies the usual Efficiency API:

// Deno efficiency
console.log( );

It is very important run scripts with the --allow-hrtime permission to allow high-resolution time size:

deno run --allow-hrtime index.js

Server-side efficiency is normally more uncomplicated to evaluate and organize as it’s depending on load, CPUs, RAM, laborious disks, and cloud provider limits. {Hardware} upgrades or procedure control choices corresponding to PM2, clustering, and Kubernetes can also be more practical than refactoring code.

The next sections be aware of client-side efficiency because of this.

Customized Efficiency Dimension

The Efficiency API can be utilized to time the execution velocity of your software purposes. You will have used or encountered timing purposes the use of Date():

const timeStart = new Date();
const timeTaken = new Date() - timeStart;

console.log(`runMyCode() achieved in ${ timeTaken }ms`);

The Efficiency API provides two number one advantages:

  1. Higher accuracy: Date() measures to the closest millisecond, however the Efficiency API can measure fractions of a millisecond (relying at the browser).
  2. Higher reliability: The consumer or OS can trade the gadget time so Date()-based metrics is not going to at all times be correct. This implies your purposes could seem specifically sluggish when clocks transfer ahead!

The Date() an identical is which returns a high-resolution timestamp which is about at 0 when the method liable for developing the report begins (the web page has loaded):

const timeStart =;
const timeTaken = - timeStart;

console.log(`runMyCode() achieved in ${ timeTaken }ms`);

A non-standard efficiency.timeOrigin assets too can go back a timestamp from 1 January 1970 despite the fact that this isn’t to be had in IE and Deno. turns into impractical when making various measurements. The Efficiency API supplies a buffer the place you’ll be able to document match for later research through passing a label identify to efficiency.mark():


init(); // run initialization purposes


funcX(); // run any other serve as


An array of all mark gadgets within the Efficiency buffer can also be extracted the use of:

const mark = efficiency.getEntriesByType('mark');

Instance end result:


    detail: null
    duration: 0
    entryType: "mark"
    name: "start:app"
    startTime: 1000
    detail: null
    duration: 0
    entryType: "mark"
    name: "start:init"
    startTime: 1001
    detail: null
    duration: 0
    entryType: "mark"
    name: "end:init"
    startTime: 1100

The efficiency.measure() manner calculates the time between two marks and likewise retail outlets it within the Efficiency buffer. You go a brand new measure identify, the beginning mark identify (or null to measure from the web page load), and the finishing mark identify (or null to measure to the present time):

efficiency.measure('init', 'birth:init', 'finish:init');

A PerformanceMeasure object is appended to the buffer with the calculated time length. To acquire this price, you’ll be able to both request an array of all measures:

const measure = efficiency.getEntriesByType('measure');

or request a measure through its identify:


Instance end result:

    detail: null
    duration: 99
    entryType: "measure"
    name: "init"
    startTime: 1001

The use of the Efficiency Buffer

In addition to marks and measures, the Efficiency buffer is used to mechanically document navigation timing, useful resource timing, and paint timing (which we’ll talk about later). You’ll download an array of all entries within the buffer:


By means of default, maximum browsers supply a buffer that retail outlets as much as 150 useful resource metrics. This must be sufficient for many checks, however you’ll be able to build up or lower the buffer restrict if wanted:

// document 500 metrics

Marks can also be cleared through identify or you’ll be able to specify an empty price to transparent all marks:


In a similar way, measures can also be cleared through identify or an empty price to transparent all:


Tracking Efficiency Buffer Updates

A PerformanceObserver can observe adjustments to the Efficiency buffer and run a serve as when particular occasions happen. The syntax shall be acquainted when you’ve used MutationObserver to reply to DOM updates or IntersectionObserver to stumble on when parts are scrolled into the viewport.

You should outline an observer serve as with two parameters:

  1. an array of observer entries which were detected, and
  2. the observer object. If important, its disconnect() manner can also be referred to as to forestall the observer.
serve as performanceCallback(record, observer) {

  record.getEntries().forEach(access => {
    console.log(`identify    : ${ access.identify }`);
    console.log(`sort    : ${ access.sort }`);
    console.log(`birth   : ${ access.startTime }`);
    console.log(`length: ${ access.length }`);


The serve as is handed to a brand new PerformanceObserver object. Its practice() manner is handed an array of Efficiency buffer entryTypes to look at:

let observer = new PerformanceObserver( performanceCallback );
observer.practice({ entryTypes: ['mark', 'measure'] });

On this instance, including a brand new mark or measure runs the performanceCallback() serve as. Whilst it simplest logs messages right here, it might be used to cause an information add or make additional calculations.

Measuring Paint Efficiency

The Paint Timing API is simplest to be had in client-side JavaScript and mechanically data two metrics which can be essential to Core Internet Vitals:

  1. first-paint: The browser has began to attract the web page.
  2. first-contentful-paint: The browser has painted the 1st important merchandise of DOM content material, corresponding to a heading or a picture.

Those can also be extracted from the Efficiency buffer to an array:

const paintTimes = efficiency.getEntriesByType('paint');

Be cautious about working this ahead of the web page has absolutely loaded; the values might not be able. Both watch for the window.load match or use a PerformanceObserver to watch paint entryTypes.

Suffering with downtime and WordPress issues? Kinsta is the webhosting answer designed to avoid wasting you time! Take a look at our options

Instance end result:

    "name": "first-paint",
    "entryType": "paint",
    "startTime": 812,
    "duration": 0
    "name": "first-contentful-paint",
    "entryType": "paint",
    "startTime": 856,
    "duration": 0

A sluggish first-paint is steadily led to through render-blocking CSS or JavaScript. The space to the first-contentful-paint might be huge if the browser has to obtain a big symbol or render advanced parts.

Useful resource Efficiency Dimension

Community timings for assets corresponding to photographs, stylesheets, and JavaScript recordsdata are mechanically recorded to the Efficiency buffer. Whilst there may be little you’ll be able to do to unravel community velocity problems (rather than lowering report sizes), it will probably assist spotlight problems with better belongings, sluggish Ajax responses, or badly-performing third-party scripts.

An array of PerformanceResourceTiming metrics can also be extracted from the buffer the use of:

const assets = efficiency.getEntriesByType('useful resource');

On the other hand, you’ll be able to fetch metrics for an asset through passing its complete URL:

const useful resource = efficiency.getEntriesByName('');

Instance end result:

    connectEnd: 195,
    connectStart: 195,
    decodedBodySize: 0,
    domainLookupEnd: 195,
    domainLookupStart: 195,
    duration: 2,
    encodedBodySize: 0,
    entryType: "resource",
    fetchStart: 195,
    initiatorType: "script",
    name: "",
    nextHopProtocol: "h3",
    redirectEnd: 0,
    redirectStart: 0,
    requestStart: 195,
    responseEnd: 197,
    responseStart: 197,
    secureConnectionStart: 195,
    serverTiming: [],
    startTime: 195,
    transferSize: 0,
    workerStart: 195

The next houses can also be tested:

  • identify: Useful resource URL
  • entryType: “useful resource”
  • initiatorType: How the useful resource used to be initiated, corresponding to “script” or “hyperlink”
  • serverTiming: An array of PerformanceServerTiming gadgets handed through the server within the HTTP Server-Timing header (your server-side software may ship metrics to the buyer for additional research)
  • startTime: Timestamp when the fetch began
  • nextHopProtocol: Community protocol used
  • workerStart: Timestamp ahead of beginning a Revolutionary Internet App Provider Employee (0 if the request isn’t intercepted through a Provider Employee)
  • redirectStart: Timestamp when a redirect began
  • redirectEnd: Timestamp after the ultimate byte of the ultimate redirect reaction
  • fetchStart: Timestamp ahead of the useful resource fetch
  • domainLookupStart: Timestamp ahead of a DNS look up
  • domainLookupEnd: Timestamp after the DNS look up
  • connectStart: Timestamp ahead of setting up a server connection
  • connectEnd: Timestamp after setting up a server connection
  • secureConnectionStart: Timestamp ahead of the SSL handshake
  • requestStart: Timestamp ahead of the browser requests the useful resource
  • responseStart: Timestamp when the browser receives the 1st byte of information
  • responseEnd: Timestamp after receiving the ultimate byte or remaining the relationship
  • length: The variation between startTime and responseEnd
  • transferSize: The useful resource dimension in bytes together with the header and compressed frame
  • encodedBodySize: The useful resource frame in bytes ahead of uncompressing
  • decodedBodySize: The useful resource frame in bytes after uncompressing

This situation script retrieves all Ajax requests initiated through the Fetch API and returns the full switch dimension and length:

const fetchAll = efficiency.getEntriesByType('useful resource')
  .filter out( r => r.initiatorType === 'fetch')
  .cut back( (sum, present) => {
    go back {
      transferSize: sum.transferSize += present.transferSize,
      length: sum.length += present.length
  { transferSize: 0, length: 0 }

Navigation Efficiency Dimension

Community timings for unloading the former web page and loading the present web page are mechanically recorded to the Efficiency buffer as a unmarried PerformanceNavigationTiming object.

Extract it to an array the use of:

const pageTime = efficiency.getEntriesByType('navigation');

…or through passing the web page URL to .getEntriesByName():

const pageTiming = efficiency.getEntriesByName(window.location);

The metrics are just like the ones for assets but in addition comprises page-specific values:

  • entryType: E.g. “navigation”
  • sort: Both “navigate”, “reload”, “back_forward,” or “prerender”
  • redirectCount: The selection of redirects
  • unloadEventStart: Timestamp ahead of the sell off match of the former report
  • unloadEventEnd: Timestamp after the sell off match of the former report
  • domInteractive: Timestamp when the browser has parsed the HTML and built the DOM
  • domContentLoadedEventStart: Timestamp ahead of report’s DOMContentLoaded match fires
  • domContentLoadedEventEnd: Timestamp after report’s DOMContentLoaded match completes
  • domComplete: Timestamp after DOM development and DOMContentLoaded occasions have finished
  • loadEventStart: Timestamp ahead of the web page load match has fired
  • loadEventEnd: Timestamp after the web page load match and all belongings are to be had

Standard problems come with:

  • A protracted lengthen between unloadEventEnd and domInteractive. This would point out a sluggish server reaction.
  • A protracted lengthen between domContentLoadedEventStart and domComplete. This would point out that web page start-up scripts are too sluggish.
  • A protracted lengthen between domComplete and loadEventEnd. This would point out the web page has too many belongings or a number of are taking too lengthy to load.

Efficiency Recording and Research

The Efficiency API means that you can collate real-world utilization information and add it to a server for additional research. You may use a third-party provider corresponding to Google Analytics to retailer the knowledge, however there’s a possibility the third-party script might be blocked or introduce new efficiency issues. Your individual answer can also be custom designed on your necessities to make sure tracking does now not affect different capability.

Be cautious of scenarios wherein statistics can’t be made up our minds — in all probability as a result of customers are on previous browsers, blockading JavaScript, or at the back of a company proxy. Figuring out what information is lacking can also be extra fruitful than making assumptions in line with incomplete knowledge.

Preferably, your research scripts gained’t negatively affect efficiency through working advanced calculations or importing huge amounts of information. Imagine using internet staff and minimizing using synchronous localStorage calls. It’s at all times imaginable to batch procedure uncooked information later.

In spite of everything, be cautious of outliers corresponding to very speedy or very sluggish units and connections that adversely have an effect on statistics. As an example, if 9 customers load a web page in two seconds however the 10th reviews a 60 2nd obtain, the common latency comes out to almost 8 seconds. A extra reasonable metric is the median determine (2 seconds) or the ninetieth percentile (9 in each 10 customers enjoy a load time of two seconds or much less).


Internet efficiency stays a essential issue for builders. Customers be expecting websites and programs to be responsive on maximum units. Seek Engine Optimization may also be affected as slower websites are downgraded in Google.
The entirety you want to understand to get began with the Efficiency API is true right here 💪Click on to Tweet
There are many efficiency tracking gear in the market, however maximum assess server-side execution speeds or use a restricted selection of succesful shoppers to pass judgement on browser rendering. The Efficiency API supplies a strategy to collate genuine consumer metrics that it might now not be imaginable to calculate every other approach.

The put up An Creation to the Efficiency API gave the impression first on Kinsta®.

WP Hosting

[ continue ]