performance.now() 是基于单调时钟的高精度时间API,精度达0.1ms,不受系统时钟影响,适用于前端性能打点;而 Date.now() 仅毫秒级且易受NTP调整干扰。

performance.now() 是浏览器提供的高精度时间测量 API,返回一个以毫秒为单位的浮点数,精度可达微秒级(通常精确到 0.1ms),非常适合做前端性能打点、耗时分析和精细化监控。
为什么不用 Date.now()?
Date.now() 只能精确到毫秒,且受系统时钟调整(如 NTP 同步)影响,可能跳变或回拨;而 performance.now() 基于单调递增的硬件时钟,不受系统时间干扰,起点是页面加载开始(navigationStart),保证相对时间差稳定可靠。
基础用法:打点 + 计算耗时
在关键逻辑前后调用 performance.now(),相减即得精确耗时:
- 开始打点:
const start = performance.now(); - 执行目标代码(如函数调用、渲染、网络请求处理等)
- 结束打点:
const end = performance.now(); - 计算耗时:
console.log(`耗时:${end - start} ms`);
结合 performance.mark / measure 做结构化打点
更规范的方式是使用 Performance Mark/Measure API,便于后续统一收集与分析:
- 打标记:
performance.mark('api-start');和performance.mark('api-end'); - 生成测量:
performance.measure('api-duration', 'api-start', 'api-end'); - 读取结果:
performance.getEntriesByName('api-duration')返回 PerformanceMeasure 对象,含duration字段 - 清除标记(可选):
performance.clearMarks()/performance.clearMeasures()
这种方式支持命名、复用、批量导出,也兼容 PerformanceObserver 动态监听。
注意事项与常见坑
注意以下几点避免误用:
- 不能跨页面/跨 iframe 直接比较
performance.now()值(各上下文独立计时起点) - SSR 环境中不可用(Node.js 无此 API),需做环境判断:
if (typeof performance !== 'undefined' && performance.now) - 大量频繁打点可能轻微影响性能,生产环境建议按需采样或聚合上报
- 部分低版本浏览器(如 IE)不支持,需兼容时 fallback 到
Date.now()(仅限对精度要求不高的场景)











