tt.performance.mark
收藏
我的收藏

基础库 1.42.1 开始支持本方法,低版本需做兼容处理。​
在性能缓冲区中使用给定名称添加一个 name 为 [name] 的性能数据。​

语法​

JavaScript
复制
tt.performance.mark(name)

参数说明​

name​

类型​
默认值​
必填​
说明​
最低支持版本​
string​
是​
期望标记的 name​
1.42.1​

返回值​

    基础库 1.42.1 ~ 1.59.0 版本 无返回值​

代码示例​

js
复制
// 创建一些标记
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

标记FMP​

还可以利用 mark 统计上报 FMP 指标,如果 name 为 FMP,基础库会自动上报数据至服务端,开发者可以在开发者后台中选择自己的小程序,即可查看 FMP 数据。​
建议在关键首屏有意义的渲染当次setData回调里标记上报,保证指标的准确性。​
js
复制
// 假设 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) => {},
});
},
});
    开发者控制台FMP数据​
在开发-性能分析-启动性能核心数据模块,切换到首次有意义渲染耗时( FMP )卡片即可。​