
A flexible library for measuring Navigation Timing, First Contentful Paint (FP/FCP), Largest Contentful Paint (LCP), First Input Delay (FID) and components lifecycle performance. Report real user measurements to your favorite analytics tool.
English | 简体中文
Why Perfume.js?
- ⏰ Supported latest Performance APIs for precise metrics
- 🔨 Cross browser tested
- 🚿 Filters out false positive/negative results
- 🤙 Only 2.1Kb gzip
- 🛰 Flexible analytics tool
- ⚡️ Waste-zero ms with requestIdleCallback strategy built-in
The latest in metrics & Real User Measurement
Perfume leverage the latest W3C Performance Drafts (e.g. PerformanceObserver), for measuring performance that matters! Also known as field data, they allow to understand what real-world users are actually experiencing.
- Navigation Timing
- First Paint (FP)
- First Contentful Paint (FCP)
- Largest Contentful Paint (LCP)
- First Input Delay (FID)
- Framework components lifecycle monitoring

With Perfume.js, you can collect those metrics and have a deep understanding everywhere in the world how your customers perceive web performance for your application. Use your favorite analytics tool to visualize the data between countries,here below how it might look a sample data for
FCP between the United States, Italy, Indonesia, and Nigeria.

Installing
npm (https://www.npmjs.com/package/perfume.js):
npm install perfume.js --save-dev
Importing library
You can import the generated bundle to use the whole library generated:
import Perfume from 'perfume.js';
Universal Module Definition:
import Perfume from 'node_modules/perfume.js/dist/perfume.umd.min.js';
Start measuring
Navigation Timing
Navigation Timing collects performance metrics for the life and timings of a network request.
Perfume helps expose some of the key metrics you might need.
- DNS lookup: When a user requests a URL, the Domain Name System (DNS) is queried to translate a domain to an IP address.
- Header size: HTTP header size
- Fetch time: Cache seek plus response time
- Worker time: Service worker time plus response time
- Total time: Request plus response time (network only)
- Download time: Response time only (download)
- Time to First Byte: The amount of time it takes after the client sends an HTTP GET request to receive the first byte of the requested resource from the server.
It is the largest web page load time component taking 40 to 60% of total web page latency.
const perfume = new Perfume({
navigationTiming: true,
analyticsTracker: ({ metricName, data }) => {
myAnalyticsTool.track(metricName, data);
})
});
First Paint (FP)
FP is the exact time the browser renders anything as visually different from what was on the screen before navigation, e.g. a background change after a long blank white screen time.
const perfume = new Perfume({
firstPaint: true,
analyticsTracker: ({ metricName, duration }) => {
myAnalyticsTool.track(metricName, duration);
})
});
First Contentful Paint (FCP)
FCP is the exact time the browser renders the first bit of content from the DOM, which can be anything from an important image, text, or even the small SVG at the bottom of the page.
const perfume = new Perfume({
firstContentfulPaint: true,
analyticsTracker: ({ metricName, duration }) => {
myAnalyticsTool.track(metricName, duration);
})
});
Largest Contentful Paint (LCP)
Largest Contentful Paint (LCP) is an important, user-centric metric for measuring
perceived load speed because it marks the point in the page load timeline when the page's main
content has likely loaded—a fast LCP helps reassure the user that the page is useful.
const perfume = new Perfume({
largestContentfulPaint: true,
analyticsTracker: ({ metricName, duration }) => {
myAnalyticsTool.track(metricName, duration);
})
});
First Input Delay (FID)
FID measures the time from when a user first interacts with your site (i.e. when they click a link, tap on a button) to the time when the browser is actually able to respond to that interaction..
const perfume = new Perfume({
firstInputDelay: true,
analyticsTracker: ({ metricName, duration }) => {
myAnalyticsTool.track(metricName, duration);
})
});
Annotate metrics in the DevTools
Performance.mark (User Timing API) is used to create an application-defined peformance entry in the browser's performance entry buffer.
const perfume = new Perfume({
analyticsTracker: ({ metricName, duration }) => {
myAnalyticsTool.track(metricName, duration);
})
});
perfume.start('fibonacci');
fibonacci(400);
perfume.end('fibonacci');

Component First Paint
This metric mark the point, immediately after creating a new component, when the browser renders pixels to the screen.
const perfume = new Perfume({
analyticsTracker: ({ metricName, duration }) => {
myAnalyticsTool.track(metricName, duration);
})
});
perfume.start('togglePopover');
$(element).popover('toggle');
perfume.endPaint('togglePopover');

Custom Logging
Save the duration and print it out exactly the way you want it.
const perfume = new Perfume({
analyticsTracker: ({ metricName, duration }) => {
myAnalyticsTool.track(metricName, duration);
}),
logPrefix: '🍹 HayesValley.js:'
});
perfume.start('fibonacci');
fibonacci(400);
const duration = perfume.end('fibonacci');
Frameworks
Angular
Wth the Angular framework, we can start configuring Perfume to collect the initial performance metrics (eg. FCP, FID). Make sure to import the PefumeModule
at first inside the NgModule
to let the PerformanceObserver work correctly.
In a large application use the @PerfumeAfterViewInit()
decorator to monitor the rendering performance of the most complex components. Avoid using it inside a NgFor, instead focus on components that include a collection of smaller components.
The NgPerfume
service exposes all the methods and property of the perfume
instance, you can annotate component lifecycles combined with APIs calls to measure how long it takes to paint the component.
import { NgPerfume, PerfumeModule, PerfumeAfterViewInit } from 'perfume.js/angular';
import { AppComponent } from './app.component';
import { AppApi } from './app-api';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.less'],
})
@PerfumeAfterViewInit('AppComponent')
export class AppComponent implements AfterViewInit {
data: AwesomeType;
constructor(public perfume: NgPerfume) {
this.perfume.start('AppComponentAfterPaint');
}
ngAfterViewInit() {
this.loadAwesomeData();
}
loadAwesomeData = async () => {
await AppApi.loadAmazingData();
this.data = AppApi.loadAwesomeData();
this.perfume.endPaint('AppComponentAfterPaint');
}
}
export const PerfumeConfig = {
firstContentfulPaint: true,
firstInputDelay: true,
};
@NgModule({
declarations: [AppComponent],
imports: [PerfumeModule.forRoot(PerfumeConfig), BrowserModule],
bootstrap: [AppComponent],
})
export class AppModule {}

React
In combination with the React framework, we can start configuring Perfume to collect the initial performance metrics (eg. FCP, FID).
Use perfume.start()
and perfume.endPaint()
into a component lifecycle mixed with APIs calls to measure how long it takes to paint the component.
import React from 'react';
import Perfume from 'perfume.js';
import { AppApi } from './AppApi';
const perfume = new Perfume({
firstContentfulPaint: true,
firstInputDelay: true
});
export default class App extends React.Component {
constructor() {
perfume.start('AppAfterPaint');
}
loadData = async () => {
await AppApi.loadAmazingData();
await AppApi.loadAwesomeData();
perfume.endPaint('AppAfterPaint');
}
render() {
const data = this.loadData();
return (
<div>
<h2>Awesome App</h2>
<div>{data}</div>
</div>
);
}
}
Analytics
Configurable analytics callback to use Perfume.js with any platform.
const perfume = new Perfume({
analyticsTracker: ({ metricName, data, duration }) => {
myAnalyticsTool.track(metricName, data, duration);
})
});
Customize & Utilities
Default Options
Default options provided to Perfume.js constructor.
const options = {
firstContentfulPaint: false,
firstPaint: false,
firstInputDelay: false,
dataConsumption: false,
largestContentfulPaint: false,
navigationTiming: false,
analyticsTracker: options => {},
logPrefix: "Perfume.js:"
logging: true,
maxMeasureTime: 15000,
maxDataConsumption: 20000,
warning: false,
debugging: false,
};
Develop
npm start
: Run npm run build
in watch mode
npm run test
: Run test suite
npm run build
: Generate bundles and typings
npm run lint
: Lints code
Articles
Credits and Specs
Made with ☕️ by @zizzamia and
I want to thank some friends and projects for the work they did:
Contributors
This project exists thanks to all the people who contribute.

Backers
Thank you to all our backers! 🙏 [Become a backer]

Copyright and license
Code and documentation copyright 2019 Leonardo Zizzamia. Code released under the MIT license. Docs released under Creative Commons.
Team