深入了解window.performance

一、window.performance 火狐報錯

在Firefox瀏覽器中,當我們使用window.performance時,可能會出現「window.performance is undefined」錯誤。這是因為Firefox沒有實現window.performance屬性。但是,我們可以使用Firefox專用的PerformanceTiming對象,其屬性和window.performance相同,以兼容Firefox。


    if (window.performance) {
        console.log("window.performance is available");
    }
    else {
        console.log("window.performance is not available");
    }
    
    if (typeof window.PerformanceTiming !== 'undefined') {
        console.log("PerformanceTiming is available");
    }
    else {
        console.log("PerformanceTiming is not available");
    }

二、window.performance.memory

該屬性返回一個對象,其中包含有關頁面的內存使用情況的信息。我們可以使用它來檢測內存泄漏。


    console.log(window.performance.memory);

三、window.performance ajax可以嗎

可以。window.performance可以用來測量Ajax請求的性能。


    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'http://example.com/api/data', true);

    var t0 = performance.now();
    xhr.onload = function() {
        var t1 = performance.now();
        console.log('Request took ' + (t1 - t0) + ' milliseconds.');
    };

    xhr.send();

四、window.performance.mark

該方法可以用於在代碼中標記時間點。我們可以使用它來測量代碼執行時間。


    performance.mark("start");

    //代碼執行

    performance.mark("end");
    performance.measure("Execution Time", "start", "end");
    var measures = performance.getEntriesByType("measure");
    console.log(measures[0].duration);

五、window.performance.n

該方法返回performance entries的數量。


    var n = performance.getEntries().length;
    console.log(n);

六、window.performance.timing

該屬性返回一個PerformanceTiming對象,該對象包含有關頁面載入過程中各種時間的信息,例如頁面載入時間、解析時間等。以下是PerformanceTiming對象中的一些屬性:

  • navigationStart
  • unloadEventStart
  • unloadEventEnd
  • redirectStart
  • redirectEnd
  • fetchStart
  • domainLookupStart
  • domainLookupEnd
  • connectStart
  • connectEnd
  • secureConnectionStart
  • requestStart
  • responseStart
  • responseEnd
  • domLoading
  • domInteractive
  • domContentLoadedEventStart
  • domContentLoadedEventEnd
  • domComplete
  • loadEventStart
  • loadEventEnd

我們可以使用PerformanceTiming對象來定位渲染過程中的瓶頸。


    var timing = performance.timing;
    console.log(timing.loadEventEnd - timing.navigationStart);

七、window.performance.now

該方法返回從性能測量開始經過的毫秒數。它是用於獲取當前時間的最簡單和最精確的方法。


    var t0 = performance.now();

    //代碼執行

    var t1 = performance.now();
    console.log("Code Execution Time: " + (t1 - t0) + "milliseconds.");

八、window.performance.mark 報錯

在使用window.performance.mark時,可能會出現「Cannot read property ‘mark’ of undefined」錯誤。這是因為瀏覽器不支持在window.performance中使用mark方法。相反,我們應該在Performance實例中使用它。


    var perf = window.performance || window.webkitPerformance || window.msPerformance || window.mozPerformance;
    if (perf) {
        perf.mark("start");
        //代碼執行
        perf.mark("end");
        perf.measure("Execution Time", "start", "end");
        var measures = perf.getEntriesByType("measure");
        console.log(measures[0].duration);
    }

九、window.performance.getEntries

該方法返回一個PerformanceEntry對象的數組,該數組包含性能測量的結果。


    var entries = performance.getEntries(); 
    console.log(entries);

以上是對window.performance的多方位闡述,如何使用它來測量頁面性能、代碼執行時間以及如何使用它來定位瓶頸。它非常有用,可以幫助我們提高用戶體驗,發現和解決性能問題。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/286641.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-23 03:48
下一篇 2024-12-23 03:48

相關推薦

  • 深入解析Vue3 defineExpose

    Vue 3在開發過程中引入了新的API `defineExpose`。在以前的版本中,我們經常使用 `$attrs` 和` $listeners` 實現父組件與子組件之間的通信,但…

    編程 2025-04-25
  • 深入理解byte轉int

    一、位元組與比特 在討論byte轉int之前,我們需要了解位元組和比特的概念。位元組是計算機存儲單位的一種,通常表示8個比特(bit),即1位元組=8比特。比特是計算機中最小的數據單位,是…

    編程 2025-04-25
  • 深入理解Flutter StreamBuilder

    一、什麼是Flutter StreamBuilder? Flutter StreamBuilder是Flutter框架中的一個內置小部件,它可以監測數據流(Stream)中數據的變…

    編程 2025-04-25
  • 深入探討OpenCV版本

    OpenCV是一個用於計算機視覺應用程序的開源庫。它是由英特爾公司創建的,現已由Willow Garage管理。OpenCV旨在提供一個易於使用的計算機視覺和機器學習基礎架構,以實…

    編程 2025-04-25
  • 深入了解scala-maven-plugin

    一、簡介 Scala-maven-plugin 是一個創造和管理 Scala 項目的maven插件,它可以自動生成基本項目結構、依賴配置、Scala文件等。使用它可以使我們專註於代…

    編程 2025-04-25
  • 深入了解LaTeX的腳註(latexfootnote)

    一、基本介紹 LaTeX作為一種排版軟體,具有各種各樣的功能,其中腳註(footnote)是一個十分重要的功能之一。在LaTeX中,腳註是用命令latexfootnote來實現的。…

    編程 2025-04-25
  • 深入剖析MapStruct未生成實現類問題

    一、MapStruct簡介 MapStruct是一個Java bean映射器,它通過註解和代碼生成來在Java bean之間轉換成本類代碼,實現類型安全,簡單而不失靈活。 作為一個…

    編程 2025-04-25
  • 深入理解Python字元串r

    一、r字元串的基本概念 r字元串(raw字元串)是指在Python中,以字母r為前綴的字元串。r字元串中的反斜杠(\)不會被轉義,而是被當作普通字元處理,這使得r字元串可以非常方便…

    編程 2025-04-25
  • 深入了解Python包

    一、包的概念 Python中一個程序就是一個模塊,而一個模塊可以引入另一個模塊,這樣就形成了包。包就是有多個模塊組成的一個大模塊,也可以看做是一個文件夾。包可以有效地組織代碼和數據…

    編程 2025-04-25
  • 深入探討馮諾依曼原理

    一、原理概述 馮諾依曼原理,又稱「存儲程序控制原理」,是指計算機的程序和數據都存儲在同一個存儲器中,並且通過一個統一的匯流排來傳輸數據。這個原理的提出,是計算機科學發展中的重大進展,…

    編程 2025-04-25

發表回復

登錄後才能評論