大网数据广告活动
Web 标准与前端开发

JS Self-Profiling API

· 52CE 我爱测

The JS Self-Profiling API enables a website to run a sampling profiler, to understand where it is spending JavaScript execution time.

JS Self-Profiling API

The JS Self-Profiling API enables a website to run a sampling profiler, to understand where it is spending JavaScript execution time.

Concepts and usage

To start a profile, a website creates a Profiler instance. As soon as the instance is created, it starts sampling the JavaScript execution context.

To stop collecting samples and retrieve the profile, the website calls Profiler.stop(). This returns a Promise which resolves to an object containing the profile data.

For example, the following function creates a profiler, then calls a function genPrimes(), then stops the profiler and retrieves the profile data:

async function profileGeneratePrimes() {
  const profiler = new Profiler({ sampleInterval: 10, maxBufferSize: 10000 });

  genPrimes();

  const trace = await profiler.stop();
  console.log(trace);
}

The profiler is a sampling profiler: this means that it periodically records (or samples) the current state of the JavaScript call stack. The profile consists of the collection of these samples. This enables you to understand where, statistically, the program is spending most of its time.

To understand exactly what a profile contains and how it is formatted, see Profile anatomy and format.

Profiling best practices

Collecting and processing profile data incurs a performance overhead of its own, and developers should be careful to manage this. Practices to minimize performance overhead include:

  • Use the maxBufferSize and sampleInterval options to control how many samples to take and how often to sample.
  • Sample for short periods in a sampled manner: for example, trace for 5 seconds out of every 60 seconds.
  • Process the samples in a web worker to avoid impacting performance on the main thread.
  • Aggregate samples on the client before sending them to a telemetry endpoint.

If the JavaScript in your site is Minification, you will need to transform the profile data based on a Source map, either on the client or on the server, before the data will be usable.

Interfaces

  • Profiler
    • : The Profiler interface is used to create profiles.

Security requirements

To use this API, the document must be served with a document policy that includes the "js-profiling" configuration point.

Specifications

Browser compatibility

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。