We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
关键词:卡顿、性能、用户体验
使用浏览器性能指标 - FPS(每秒帧数)
requestAnimationFrame
let frameCount = 0; let lastTime = 0; const fpsArray = []; function calculateFps() { const now = performance.now(); frameCount++; if (now - lastTime >= 1000) { const fps = frameCount; fpsArray.push(fps); frameCount = 0; lastTime = now; } requestAnimationFrame(calculateFps); } calculateFps();
calculateFps
performance.now
frameCount
fpsArray
监测 Long Tasks(长任务)
PerformanceObserver
const observer = new PerformanceObserver((list) => { for (const entry of list.getEntries()) { if (entry.duration > 50) { console.log("发现长任务:", entry); } } }); observer.observe({ entryTypes: ["longtask"] });
longtask
分析 First Input Delay(首次输入延迟 - FID)
let startTime; const observer = new PerformanceObserver((list) => { for (const entry of list.getEntries()) { if (entry.name === "first - input") { const delay = entry.startTime - startTime; console.log("首次输入延迟:", delay); } } }); observer.observe({ entryTypes: ["first - input"] }); document.addEventListener("click", (event) => { startTime = performance.now(); // 模拟一个任务,可能导致延迟 setTimeout(() => { console.log("任务完成"); }, 100); });
startTime
first - input
The text was updated successfully, but these errors were encountered:
No branches or pull requests
关键词:卡顿、性能、用户体验
使用浏览器性能指标 - FPS(每秒帧数)
requestAnimationFrame
函数来计算 FPS。以下是一个简单的 JavaScript 示例,用于监测页面的 FPS:calculateFps
,它使用performance.now
函数获取当前时间戳。每执行一次requestAnimationFrame
回调函数(通常每秒执行约 60 次),frameCount
就加 1。当时间间隔达到 1000 毫秒(1 秒)时,计算出 FPS 并存储在fpsArray
中。通过分析fpsArray
中的数据,可以了解页面在一段时间内的 FPS 情况,从而判断是否卡顿。监测 Long Tasks(长任务)
PerformanceObserver
来监测 Long Tasks。以下是一个示例:PerformanceObserver
对象,它会监听longtask
类型的性能条目。当发现执行时间超过 50 毫秒的任务时,会在控制台打印相关信息,包括任务的开始时间、持续时间等,通过这些信息可以定位导致卡顿的代码部分。分析 First Input Delay(首次输入延迟 - FID)
PerformanceObserver
和相关的性能 API 来测量 FID。以下是一个示例:startTime
,然后通过PerformanceObserver
监听first - input
类型的性能条目。当监听到这个条目时,计算并打印出 FID。通过这种方式,可以评估用户操作后的响应延迟情况。The text was updated successfully, but these errors were encountered: