tt.performance.mark
基础库 1.42.1 开始支持本方法,低版本需做兼容处理。
在性能缓冲区中使用给定名称添加一个 name 为 [name] 的性能数据。
语法
tt.performance.mark(name)
参数说明
name
类型 | 默认值 | 必填 | 说明 |
---|---|---|---|
string | 是 | 期望标记的 name |
返回值
- 基础库 1.60.0 版本及以上返回 PerformanceEntry
- 基础库 1.42.1 ~ 1.59.0 版本 无返回值
代码示例
- 常规用法
// 创建一些标记
tt.performance && tt.performance.mark("bytedance");
tt.performance && tt.performance.mark("bytedance");
tt.performance && tt.performance.mark("toutiao");
tt.performance && tt.performance.mark("toutiao");
tt.performance && tt.performance.mark("douyin");
tt.performance && tt.performance.mark("douyin");
// 获取所有的 PerformanceMark 条目
const allEntries = tt.performance && tt.performance.getEntriesByType("mark");
console.log(allEntries.length); // 6
// 获取所有的名为 "bytedance" PerformanceMark 条目
const bytedanceEntries = tt.performance && tt.performance.performance.getEntriesByName("bytedance");
console.log(bytedanceEntries.length); // 2
// 删除所有标记。
tt.performance && tt.performance.clearMarks();
console.log(tt.performance && tt.performance.getEntriesByType("mark")); // 0
- 利用 mark 统计、上报 FMP 指标
// 假设 FMP 依赖某个接口的数据,接口返回数据后 setData 更新页面后上报 FMP
Page({
data: {
pages: [],
},
onLoad() {
tt.request({
url: `${developer_api_url}`,
success: (res) => {
this.setData(
{
pages: res.data,
},
() => {
// 统计 FMP
tt.performance && tt.performance.mark("FMP");
},
);
},
fail: (err) => {},
});
},
});
Bug & Tip
- Tip: 如果 name 为 FMP, 基础库会自动上报数据至服务端,开发者可以在开发者后台中选择自己的小程序,然后在数据分析-性能分析-质量监控可以查看 FMP 数据
点击纠错
评价此篇文档