基础库
PerformanceObserver 对象,用于监听性能相关事件
获取当前支持的所有性能指标类型
开始监听
设置 type(string) 监听单个类型的指标,设置 entryTypes(string) 监听多个类型指标。 type和entryTypes不能同时使用
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
paint | string | 否 | ||
framework | string | 否 | ||
script | string | 否 | ||
network | string | 否 |
停止监听
// 检查当前支持的性能指标类型 const supportedTypes = xhs.PerformanceObserver.supportedEntryTypes; console.log('支持的性能指标类型:', supportedTypes); // 创建 PerformanceObserver 实例 const observer = new xhs.PerformanceObserver((list) => { const entries = list.getEntries(); entries.forEach((entry) => { console.log('性能指标:', entry); }); }); // 开始监听单个类型的性能指标(例如 'paint') observer.observe({ type: 'paint' }); // 或者监听多个类型的性能指标 observer.observe({ entryTypes: ['script', 'network'] }); // 停止监听 setTimeout(() => { observer.disconnect(); console.log('已停止性能指标监听'); }, 10000); // 10秒后停止监听