Coder Social home page Coder Social logo

area73 / perfume.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zizzamia/perfume.js

0.0 2.0 0.0 5.07 MB

A flexible JavaScript library for measuring First Contentful Paint (FP/FCP), First Input Delay (FID) and components lifecycle performance (eg. Angular, React). Report real user measurements to Google Analytics or your ideal tracking tool.

Home Page: http://perfumejs.com

License: MIT License

TypeScript 100.00%

perfume.js's Introduction

NPM version Build Status NPM Downloads Test Coverage JS gzip size

A flexible library for measuring First Contentful Paint (FP/FCP), First Input Delay (FID) and components lifecycle performance. Report real user measurements to Google Analytics or your ideal tracking tool.



Why Perfume.js?

  • โฐ Latest Performance APIs for precise metrics
  • ๐Ÿ”จ Cross browser tested
  • ๐Ÿšฟ Filters out false positive/negative results
  • ๐Ÿ”ญ Browser tracker built-in
  • ๐Ÿค™ Support for async/await syntax
  • ๐Ÿ›ฐ Flexible tracking tool
  • โšก๏ธ Waste-zero ms with Idle Until Urgent strategy built-in

User-centric performance metrics

Perfume leverage the latest W3C Performance Drafts (like PerformanceObserver), for measuring performance that matters! โšก๏ธ

  • First Paint (FP)
  • First Contentful Paint (FCP)
  • First Input Delay (FID)
  • Framework components lifecycle monitoring

first paint and first input delay

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 by this starter:

import Perfume from 'perfume.js';

Universal Module Definition:

import Perfume from 'node_modules/perfume.js/dist/perfume.umd.min.js';

Start measuring

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
});
// Perfume.js: First Paint 1482.00 ms

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
});
// Perfume.js: First Contentful Paint 2029.00 ms

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
});
// Perfume.js: First Input Delay 3.20 ms

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.

perfume.start('fibonacci');
fibonacci(400);
perfume.end('fibonacci');
// Perfume.js: fibonacci 0.14 ms

Performance Mark

Component First Paint

This metric mark the point, immediately after creating a new component, when the browser renders pixels to the screen.

perfume.start('togglePopover');
$(element).popover('toggle');
perfume.endPaint('togglePopover');
// Perfume.js: togglePopover 10.54 ms

Performance

Custom Logging

Save the duration and print it out exactly the way you want it.

const perfume = new Perfume({
  logPrefix: '๐Ÿน HayesValley.js:'
});
perfume.start('fibonacci');
fibonacci(400);
const duration = this.perfume.end('fibonacci');
perfume.log('Custom logging', duration);
// ๐Ÿน HayesValley.js: Custom logging 0.14 ms

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) {
    // Start measure component time to paint
    this.perfume.start('AppComponentAfterPaint');
  }

  ngAfterViewInit() {
    this.loadAwesomeData();
  }

  loadAwesomeData = async () => {
    await AppApi.loadAmazingData();
    this.data = AppApi.loadAwesomeData();
    // End measure component time to paint
    this.perfume.endPaint('AppComponentAfterPaint');
  }
}

// Perfume.js config, supports AOT and DI
export const PerfumeConfig = {
  firstContentfulPaint: true,
  firstInputDelay: true,
};

@NgModule({
  declarations: [AppComponent],
  imports: [PerfumeModule.forRoot(PerfumeConfig), BrowserModule],
  bootstrap: [AppComponent],
})
export class AppModule {}

Angular Performance Decorator

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() {
    // Start measure component time to paint
    perfume.start('AppAfterPaint');
  }

  loadData = async () => {
    await AppApi.loadAmazingData();
    await AppApi.loadAwesomeData();
    // End measure component time to paint
    perfume.endPaint('AppAfterPaint');
  }

  render() {
    const data = this.loadData();
    return (
      <div>
        <h2>Awesome App</h2>
        <div>{data}</div>
      </div>
    );
  }
}

Analytics

Google Analytics

To enable Perfume to send your measures to Google Analytics User timing, set the option enable:true and a custom user timing variable timingVar:"name".

const perfume = new Perfume({
  googleAnalytics: {
    enable: true,
    timingVar: 'userId'
  }
});

Performance Analytics

Generic analytics platform support

Configurable analytics callback to use Perfume.js with any platform.

const perfume = new Perfume({
  analyticsTracker: (metricName, duration, browser) => {
    myAnalyticsTool.track(metricName, duration, browser.name, browser.os);
  })
});

Customize & Utilities

Default Options

Default options provided to Perfume.js constructor.

const options = {
  // Metrics
  firstContentfulPaint: false,
  firstPaint: false,
  firstInputDelay: false,
  // Analytics
  analyticsTracker: undefined,
  browserTracker: false,
  googleAnalytics: {
    enable: false,
    timingVar: 'name',
  },
  // Logging
  logPrefix: 'Perfume.js:',
  logging: true,
  maxMeasureTime: 15000,
  warning: false,
  debugging: false,
};

Utilities

Perfume.js expose some methods and properties which may be useful to people extending the library.

const perfume = new Perfume({
  firstContentfulPaint: true,
  firstInputDelay: true,
});

// Values
perfume.firstPaintDuration;
perfume.firstContentfulPaintDuration;
perfume.firstInputDelayDuration;

// Aync Values
const durationFCP = await perfume.observeFirstContentfulPaint;
const durationFID = await perfume.observeFirstInputDelay;

// Send Custom User timing measure to Google Analytics
perfume.sendTiming(metricName, durationFCP);

Develop

  • npm start: Run npm run build in watch mode
  • npm run test: Run test suite
  • npm run test:watch: Run test suite in interactive watch mode
  • 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


Leonardo Zizzamia

perfume.js's People

Contributors

zizzamia avatar lorenzodianni avatar andarist avatar dan-harris avatar michaeldfallen avatar oliverturner avatar planser avatar noriste avatar stevermeister avatar tambulkar avatar http-teapot avatar devilicecream avatar monkeywithacupcake avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.