PerformanceObserver自動(dòng)獲取首屏?xí)r間實(shí)現(xiàn)示例

介紹
PerformanceObserver 可用于獲取性能相關(guān)的數(shù)據(jù),例如首幀fp、首屏fcp、首次有意義的繪制 fmp等等。
構(gòu)造函數(shù)
PerformanceObserver() 創(chuàng)建并返回一個(gè)新的 PerformanceObserver 對(duì)象。
提供的方法
PerformanceObserver.observe()
當(dāng)記錄的性能指標(biāo)在指定的 entryTypes 之中時(shí),將調(diào)用性能觀察器的回調(diào)函數(shù)。
PerformanceObserver.disconnect()
停止性能觀察者回調(diào)接收到性能指標(biāo)。
PerformanceObserver.takeRecords()
返回存儲(chǔ)在性能觀察器中的性能指標(biāo)的列表,并將其清空。
重點(diǎn)我們看看observer.observe(options);
options
一個(gè)只裝了單個(gè)鍵值對(duì)的對(duì)象,該鍵值對(duì)的鍵名規(guī)定為 entryTypes。entryTypes 的取值要求如下:
entryTypes 的值:一個(gè)放字符串的數(shù)組,字符串的有效值取值在性能條目類型 中有詳細(xì)列出。如果其中的某個(gè)字符串取的值無(wú)效,瀏覽器會(huì)自動(dòng)忽略它。
另:若未傳入 options 實(shí)參,或傳入的 options 實(shí)參為空數(shù)組,會(huì)拋出 TypeError。
實(shí)例
<script>
const observer = new PerformanceObserver((list) => {
for(const entry of list.getEntries()){
console.groupCollapsed(entry.name);
console.log(entry.entryType);
console.log(entry.startTime);
console.log(entry.duration);
console.groupEnd(entry.name);
}
})
observer.observe({entryTypes:['longtask','frame','navigation','resource','mark','measure','paint']});
</script>
獲取結(jié)果

根據(jù)打印結(jié)果我們可以推測(cè)出來(lái):
entryTypes里的值其實(shí)就是我們告訴PerformanceObserver,我們想要獲取的某一方面的性能值。例如傳入paint,就是說(shuō)我們想要得到fcp和fp。
所以我們看打印,它打印出來(lái)了fp和fcp

這里有必要解釋一下什么是fp,fcp,fpm
TTFB:Time To First Byte,首字節(jié)時(shí)間
FP:First Paint,首次繪制,繪制Body
FCP:First Contentful Paint,首次有內(nèi)容的繪制,第一個(gè)dom元素繪制完成
FMP:First Meaningful Paint,首次有意義的繪制
TTI:Time To Interactive,可交互時(shí)間,整個(gè)內(nèi)容渲染完成
不懂?看圖!

FP僅有一個(gè)div根節(jié)點(diǎn)
FCP包含頁(yè)面的基本框架,但沒(méi)有數(shù)據(jù)內(nèi)容
FMP包含頁(yè)面的所有元素及數(shù)據(jù)
Wow!恍然大悟!
實(shí)際使用
好了,我們?cè)趯?shí)際項(xiàng)目中怎么取獲取呢?可以看看我的實(shí)現(xiàn)參考一下下:
// 使用 PerformanceObserver 監(jiān)聽(tīng) fcp
if (!!PerformanceObserver){
try {
const type = 'paint';
if ((PerformanceObserver.supportedEntryTypes || []).includes(type)) {
observer = new PerformanceObserver((entryList)=>{
for(const entry of entryList.getEntriesByName('first-contentful-paint')){
const { startTime } = entry;
console.log('[assets-load-monitor] PerformanceObserver fcp:', startTime);
// 上報(bào)startTime操作
}
});
observer.observe({
entryTypes: [type],
});
return;
}
} catch (e) {
// ios 不支持這種entryTypes,會(huì)報(bào)錯(cuò) https://caniuse.com/?search=PerformancePaintTiming
console.warn('[assets-load-monitor] PerformanceObserver error:', (e || {}).message ? e.message : e);
}
}
這里用了判斷是否可以使用PerformanceObserver,不能使用的話,我們是用其他方法的,例如MutationObserver,這個(gè)我們我們后面再講。

參考:
https://developer.mozilla.org/zh-CN/docs/Web/API/PerformanceObserver/observe
http://www.dbjr.com.cn/article/95836.htm
以上就是PerformanceObserver獲取首屏?xí)r間實(shí)現(xiàn)示例的詳細(xì)內(nèi)容,更多關(guān)于PerformanceObserver首屏?xí)r間的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
微信小程序 POST請(qǐng)求(網(wǎng)絡(luò)請(qǐng)求)詳解及實(shí)例代碼
這篇文章主要介紹了微信小程序 POST請(qǐng)求(網(wǎng)絡(luò)請(qǐng)求)詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-11-11
微信小程序 后臺(tái)登錄(非微信賬號(hào))實(shí)例詳解
這篇文章主要介紹了微信小程序 后臺(tái)登錄(非微信賬號(hào))實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
微信小程序 高德地圖SDK詳解及簡(jiǎn)單實(shí)例(源碼下載)
這篇文章主要介紹了微信小程序 高德地圖詳解及簡(jiǎn)單實(shí)例(源碼下載)的相關(guān)資料,需要的朋友可以參考下2017-01-01
lodash內(nèi)部方法getData和setData實(shí)例解析
本篇章我們將了解lodash里內(nèi)部關(guān)于Data的操作方法,重點(diǎn)關(guān)注getData、setData兩個(gè)內(nèi)部方法,同時(shí)由實(shí)現(xiàn)上引申其他內(nèi)部封裝的方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
JavaScript生成器函數(shù)Generator?Functions優(yōu)缺點(diǎn)特性詳解
這篇文章主要為大家介紹了JavaScript生成器函數(shù)Generator?Functions的特性及優(yōu)點(diǎn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
微信小程序 http請(qǐng)求封裝詳解及實(shí)例代碼
這篇文章主要介紹了微信小程序 http請(qǐng)求封裝詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02

