javascript - 如何使用性能观察者 api 查找长任务信息
问题描述
我正在学习性能观察者 API,我想知道代码中有多少长任务,所以我写了这段代码:
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
console.log(entry);
}
});
observer.observe({entryTypes: ['longtask']});
现在我得到的就是这个
谁能告诉我如何找出哪个任务是长任务我的意思是什么导致它或它发生在哪里?
解决方案
除非您正在查看默认标记,例如在 Chrome DevTools 的“性能”选项卡中可见的首次绘制、脚本、XMLHttpRequest...等,否则我相信您需要设置命名标记以利用用户计时 API .
performance.mark("start_test") // start the timer
await doWork(); // the function you want to test
performance.mark("end_test") // start the timer
performance.getEntriesByName("test").forEach(entry => {
// Display each reported measurement on console
if (console) {
console.log("Name: " + entry.name +
", Type: " + entry.entryType +
", Start: " + entry.startTime +
", Duration: " + entry.duration + "\n");
}
})
推荐阅读
- javascript - 当标题是键和值的混合时,将数据从 json 附加到表
- r - 在 R 循环中删除列表中的项目
- node.js - Express - 在路由级别有条件地使用应用程序级别的中间件
- python - Python csv - 根据另一列的值计算列中的字符串数
- reactjs - 刷新后保存状态
- javascript - 有没有办法确定所选实例是否是导入的 SVG?
- c# - 在 dot net core API 中设置具有相同状态响应的自定义错误代码
- swift - “uiframework”是无法访问的,因为它没有入口点,也没有运行时访问的标识符
- google-apps-script - Google sheet + App script:从下拉菜单中选择项目,但将所选值替换为另一个值
- django - Django在不知道相关模型的情况下以一对一的关系获取相关对象