你瞭解 Performance Timeline Level 2 嗎?

什麼是 Performance Timeline?

Performance Timeline是W3C性能小組提出的一個規範,定義了讓開發者在應用整個生命週期內收集各類性能指標的接口。javascript

最新的Performance Timeline Level 2標準,取代了初版Performance Timeline標準,它包括瞭如下三點:html

  • 擴展了Performance接口的基本定義
  • 在Web Workers中暴露了PerformanceEntry
  • 增長了PerformanceObserver的支持

Performance Timeline 在 Node.js

值得注意的是,在Node.js裏一樣遵循W3C的Performance Timeline規範,實現了 Performance Hooks 接口。Performance Hooks 的API和現代瀏覽器中實現的 Performance API 是一致的。java

在瀏覽器中,咱們可使用 window 對象取得window.performancewindow.PerformanceObserver ,而在 Node.js 程序中須要 perf_hooks 取得性能對象,以下:node

const { PerformanceObserver, performance } = require('perf_hooks');
複製代碼

瀏覽器兼容性

  • performance在瀏覽器中的兼容性
  • PerformanceObserver在瀏覽器中的兼容性
    PerformanceObserver兼容性

W3C性能小組鼓勵開發人員在瀏覽器兼容性容許的狀況下,儘量使用 PerformanceObserver。另外,新的性能API和指標可能只能經過PerformanceObserver 接口得到。json

performance 和 PerformanceObserver 的使用

window.performance

使用window.performance,咱們能夠這樣去度量某個函數的性能。api

function init() {
      performance.mark("startWork");
      doWork(); 
      performance.mark("endWork");
      performance.measure("work", "startWork", "endWork")
      measurePerf();
    }
    function measurePerf() {
      performance
        .getEntries()
        .map(entry => JSON.stringify(entry, null, 2))
        .forEach(json => console.log(json));
    }
複製代碼

在上述代碼中,咱們使用了performance.markperformance.measure來記錄性能記錄的數據。瀏覽器

performance.measure方法會根據 startMark 和 endMark 對應的,由 performance.mark產生的兩條記錄,來產生一條 entryType 爲 'measure' 的新記錄,並計算運行時長。bash

而後代碼裏使用了performance.getEntries來獲取瀏覽器緩衝區中全部的性能數據記錄。異步

固然,咱們也可使用performance.getEntriesByName獲取指定 entryType 的性能記錄。函數

window.PerformanceObserver

正如上文所述,咱們要想得到某項性能記錄,須要知道指定的性能事件已經發生(或者使用定時輪訓的方式),主動調用performance.getEntries或者performance.getEntriesByName來得到。

爲了解決這個問題,在Performance Timeline Level 2中,除了擴展了Performance的基本定義之外,還增長了PerformanceObserver接口。

顧名思義,PerformanceObserver在瀏覽器內部對Performance實現了觀察者模式,也是現代瀏覽器支持的幾個 Observer 之一。

它解決了如下3點問題:

  • 避免不知道性能事件啥時候會發生,須要重複輪訓timeline獲取記錄。
  • 避免產生重複的邏輯去獲取不一樣的性能數據指標
  • 避免其餘資源須要操做瀏覽器性能緩衝區時產生競態關係。

在兼容Performance Timeline Level 2的瀏覽器或者 Node.js 中,能夠這樣寫:

const userTimingObserver = new PerformanceObserver(list => {
  list
    .getEntries()
    .map(({ name, entryType, startTime, duration }) => {
      const obj = {
        "Duration": duration,
        "Entry Type": entryType,
        "Name": name,
        "Start Time": startTime,
      };
      return JSON.stringify(obj, null, 2);
    })
    .forEach(console.log);
  userTimingObserver.disconnect();
});
userTimingObserver.observe({entryTypes: ["mark", "measure"]});
複製代碼

另外有必要介紹一下 performanceObserver.observe 函數,它接受兩個參數entryTypesbuffered

  • entryTypes 聲明須要觀察哪幾類性能數據
  • buffered 聲明回調函數是當即同步執行仍是異步執行,例子以下
const {
  performance,
  PerformanceObserver
} = require('perf_hooks');

const obs = new PerformanceObserver((list, observer) => {
  // 同步執行三次. 每次`list` 僅包含一項 item.
});
obs.observe({ entryTypes: ['mark'] });

for (let n = 0; n < 3; n++)
  performance.mark(`test${n}`);
複製代碼
const {
  performance,
  PerformanceObserver
} = require('perf_hooks');

const obs = new PerformanceObserver((list, observer) => {
  // 執行一次. `list` 包含3個 items.
});
obs.observe({ entryTypes: ['mark'], buffered: true });

for (let n = 0; n < 3; n++)
  performance.mark(`test${n}`);
複製代碼

總結

Performance Timeline Level 2規範中,擴充了performance的定義,並增長了PerformanceObserver的支持。

相比PerformanceObserverwindow.performance在瀏覽器中兼容性較好,另外Performance Hooks在 Node.js 中仍然處於 Experimental 階段。

推薦在瀏覽器兼容的狀況下使用PerformanceObserver,使用這種觀察者模式能夠解決主動調用的問題,並且更爲優雅。

相關文章
相關標籤/搜索