创建全局性能事件监听器
回调函数
PerformanceObserver
// page/index/index
Page({
onLoad() {
// 假如业务方想统计fp、fcp、fmp
const performance = xhs.getPerformance()
this.observer = performance.createObserver((entryList) => {
const entries = entryList.getEntries()
this.report(entries)
})
this.observer.observe({ entryTypes: ['paint'] })
// or this.observer.observe({ type: 'paint' })
},
report(entries) {
const fp = entries.filter(item => item.name === 'firstPaint')[0]
if (fp) {
const {
startTime, // fp的时间戳
duration, // 无
value, // performance.timing.navigationStart - startTime的这段时间
} = fp
}
// 业务方do something
},
onUnload() {
this.observer.disconnect()
}
})