首页 > 解决方案 > 持续时间为负的 PerformanceObserver 标记 api

问题描述

我正在尝试准确marks了解我的 React 代码的某些 JS api 的执行时间。我在规格中看到了这个

Set entry's duration attribute to the duration from start time to end time. 
The resulting duration value MAY be negative.

我很好奇为什么其中一些durations是负面的。也就是说,我开始markin 代码,并mark在一定以后的时间结束。我的全部目的是捕捉持续时间,所以我对结果有点困惑。

const k = 'EXAMPLE_KEY';
const start = `start:${k}`;
const end = `end:${k}`;


//// Parent React Component
performance.mark(start);

/// Child Component, which happens to be an img
const onLoad = () => {
   performance.mark(end);
   const result = performance.measure(k, start, stop);
   /// Yields negative duration
}

当 aduration为负时,有哪些注意事项?

这是一个非常相似的 SO 问题,但该问题没有答案,也没有手动处理start/stop。任何帮助表示赞赏!

标签: javascriptreactjsperformanceperformance-testing

解决方案


推荐阅读