Profiler

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

实验性: 这是一项实验性技术
在生产中使用此技术之前,请仔细检查浏览器兼容性表格

Profiler 接口是 JS 自定义性能分析 API 的一部分,它使您能够对 Web 应用程序执行的某个部分创建 性能分析

构造函数

Profiler() 实验性

创建一个新的 Profiler 对象,并开始收集样本。

实例方法

Profiler.stop() 实验性

停止性能分析器,返回一个 Promise,该 Promise 解析为 性能分析结果。

事件

samplebufferfull

当性能分析已记录足够的样本以填满其内部缓冲区时触发。

示例

记录性能分析

以下代码分析 doWork() 操作,并记录结果。

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

doWork();

const profile = await profiler.stop();
console.log(JSON.stringify(profile));

分析页面加载

以下代码分析脚本首次运行与窗口 load 事件触发之间的时间。

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

window.addEventListener("load", async () => {
  const profile = await profiler.stop();
  console.log(JSON.stringify(profile));
});

规范

规范
JS 自我剖析 API
# the-profiler-interface

浏览器兼容性