New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

first-important-paint

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

first-important-paint

Measure the time taken to paint the first important element.

latest
Source
npmnpm
Version
1.0.1
Version published
Weekly downloads
10
150%
Maintainers
1
Weekly downloads
 
Created
Source

first-important-paint

First Important Paint (FIP) measures the time taken to paint the first important element to screen.

Largest Contentful Paint (LCP) measures the time it takes to paint the largest element—<img>, <image>, <video>, CSS's background-image and text elements—to screen. However, the largest element is not always the most important one. If the most important element is a <table> consisting of multiple smaller elements; then the LCP metric may not be representative of the user experience.

First Important Paint aims to solve that limitation by allowing you to measure the timing for any element using requestAnimationFrame and checking when an element is visible on the page. It can be used in combination with LCP and the Element Timing API.

FIP works on all modern browsers, including Safari.

Installation:

npm -i -s first-important-paint

Usage

To begin measuring First Important Paint you are required to import the first-important-paint as early as possible in your application's JavaScript file.

main.js

import {start} from "first-important-paint";
start();

You can then mark important elements using the important attribute.

index.html

<div important>
  <ul>
    <li>Item #1</li>
    <li>Item #2</li>
  </ul>
</div>

When the first important element is rendered on screen, the browser will create a performance.mark entry with the name first-important-paint. This is visible on DevTools and can be retrieved later using the PerformanceObserver.

new PerformanceObserver((entryList) => {
  for (const entry of entryList.getEntries()) {
    if (entry.name === "first-important-paint") {
      console.log(entry);

      const { name, startTime, detail } = entry;
      const { id, nodeName, src } = detail;

      // Test sending the metric to an analytics endpoint.
      navigator.sendBeacon(
        `/collect`,
        JSON.stringify({entryType: "first-important-paint", renderTime: startTime, id, nodeName, url: src})
      );
    }
  }
}).observe({ type: "mark", buffered: true });

Options

You can override the default configurations by passing parameters to the start method. Below are the supported options:

OptionTypeDescription
markNamestringThe name to be used when creating the performance.mark (Default: first-important-paint)
selectorstringThe CSS selector to use to identify important elements. (Default: [important])
timeoutnumberThe maximum time, in milliseconds, to search for the element. (Default: 60000)

Quality

To check the quality of the metric, I ran several tests and document my research.

The tests indicate that FIP correlates with LCP and Element Timing and is stable and elastic, but tends to underreport.

Known limitations

  • FIP will wait for all fonts to finish downloading. This means that if the element you are measuring does not use any fonts but other elements on the page do, then it will not fire until all fonts have finished downloading.
  • If the FIP element is offscreen it will still be reported.
  • It is not tested on <video> elements.
  • When a <picture> element is marked as important, the metric will correctly measure the time the <picture> element renders but will log the src of the <img> not necessarily the image rendered.
  • FIP does not factor in the GPU.

FAQ

Can I use it with ReactJS?

Yes, it is supported on any JavaScript framework, including ReactJS.

Is there a performance overhead when using FIP?

FIP was developed with minimal overhead. It uses requestAnimationFrame and postmessage, does not block the main thread, and is less than 1KB minified. My tests indicate that it has no impact on LCP.

Which browsers are supported?

Supported on all major browsers, including Chrome, Firefox and Safari. CanIUse data.

Contributing

Anyone and everyone is welcome to contribute to this project and leave feedback. Please take a moment to review the guidelines for contributing.

This software is released under the terms of the MIT license.

Keywords

performance

FAQs

Package last updated on 20 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts