@fastly/performance-observer-polyfill

中文文档
2.0.0发布于 2021/02/23MITTypeScriptESM + CJS近 30 天下载 25.6万

🔎 PerformanceObserver Polyfill

原始使用文档
文档目录11 节
  1. Quick links
  2. Installation
  3. Usage:
  4. As a polyfill
  5. Usage: As a ponyfill
  6. Caveats
  7. Development
  8. Requirements
  9. Install
  10. Running
  11. License

🔎 PerformanceObserver Polyfill

Travis gzip size

The PerformanceObserver interface is a JavaScript API that can be used to observe the Performance Timeline to be notified of new performance metrics as they are recorded.

This polyfill allows consumers to use the PerformanceObserver interface within browser environments, which have basic Performance Timeline support (I.e. window.performance.getEntries()), but don't have observer support.

The polyfill works by falling back to polling the Performance Timeline on a given interval and calling all subscribed observers with the resulting set of entires.

Installation

npm install --save @fastly/performance-observer-polyfill

Usage:

As a polyfill

This automatically "installs" PerformanceObserverPolyfill as window.PerformanceObserver() if it detects PerformanceObserver isn't supported:

import '@fastly/performance-observer-polyfill/polyfill'

// PerformanceObserver is now available globally!
const observer = new PerformanceObserver((list) => {});
observer.observe({entryTypes: ['resource']});

Usage: As a ponyfill

With a module bundler like rollup or webpack, you can import @fastly/performance-observer-polyfill to use in your code without modifying any globals:

// using JS Modules:
import PerformanceObserver from '@fastly/performance-observer-polyfill'

// or using CommonJS:
const PerformanceObserver = require('@fastly/performance-observer-polyfill')

// usage:
const observer = new PerformanceObserver((list) => {});
observer.observe({entryTypes: ['resource']})

Caveats

As the polyfill implements the PerformanceObserver interface by falling back to polling the Performance Timeline via a call to window.performance.getEntries() we are limited to only expose timeline entry types that are supported by getEntries(). Therefore the polyfill can only be used to observe the entry types: navigation, resource and mark. Newer entry types such as paint are only exposed by the native PerformanceObserver implementation and thus not polyfillable.

Development

Requirements

  • Node.js >= 10

Install

git clone [email protected]:fastly/performance-observer-polyfill.git
cd performance-observer-polyfill
npm install
npm run build

Running

Most actions you'd like to perform whilst developing performance-observer-polyfill are defined as NPM scripts tasks and can be invoked using npm run {task}.

A list of all commands and their description can be found below.

NameDescription
buildCompiles the application for production environments
build:devCompiles the application for development
lintLints the source files for style errors using ESLint and automatically formats the source files using prettier
testRuns the unit test suite

License

MIT

版本与发布记录

@fastly/performance-observer-polyfill 共发布 12 个版本,首次发布于 2019/06/13;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.0latest2021/02/235 年前
2.0.0-pre-1.3next2021/02/165 年前
2.0.0-pre-1.22021/02/165 年前
2.0.0-pre-1.12021/02/165 年前
2.0.0-pre-1.02021/02/115 年前
2.0.0-pre-12021/02/105 年前

关于 @fastly/performance-observer-polyfill 的常见问题

@fastly/performance-observer-polyfill 是什么?

🔎 PerformanceObserver Polyfill

如何安装 @fastly/performance-observer-polyfill?

在项目目录运行 npm install @fastly/performance-observer-polyfill,或使用 pnpm add / yarn add / bun add。

@fastly/performance-observer-polyfill 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fastly/performance-observer-polyfill 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@fastly/performance-observer-polyfill 可以商用吗?

@fastly/performance-observer-polyfill 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fastly/performance-observer-polyfill 的下载量有多少?

根据 npm 官方下载统计,@fastly/performance-observer-polyfill 在 2026/10/15 – 2026/11/13 这 30 天内被下载 256,262 次,日均约 8,542 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fastly/performance-observer-polyfill 还在维护吗?

最新版本 2.0.0 发布于 2021/02/23(5 年前);至今共发布 12 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/06/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。