Shoora
4/13/2019 - 11:27 PM

Page Performance API

Page Performance API

 function add()
{
    for(var i = 0; i < 1000000; i++)
    {
        //some overhead        
    }
    return 12 + 89;
}
window.onload = function()
{
    if("performance" in window)
    {
        if("mark" in window.performance)
        {
            document.getElementById("main_content").innerHTML = "Page Performance API is supported. Check JS console for results of user timing API";
            //note the present time into "mark1" identifier.
            performance.mark("mark1");
            add();
            //note the present time into "mark2" identifier.
            performance.mark("mark2");
            add();
            //note the present time into "mark3" identifier.
            performance.mark("mark3");
            add();
            //note the present time into "mark4" identifier.
            performance.mark("mark4");
            //now calculate the difference between the notes time and stored in identifier "measure1", "measure2" and "measure3"
            performance.measure("measure1", "mark1", "mark2");
            performance.measure("measure2", "mark2", "mark3");
            performance.measure("measure3", "mark3", "mark4");
            //"mark" identifies all marks
            var performanceMarks = performance.getEntriesByType("mark");
            var info = '';
            for (i = 0; i < performanceMarks.length; i++) {
                info = "Name: " + performanceMarks[i].name + " - " + "Start Time: " + performanceMarks[i].startTime + "";
                console.log(info);
            }
            //"measure" identifies all measures
            var performanceMeasure = performance.getEntriesByType("measure");
            var info = '';
            for (i = 0; i < performanceMeasure.length; i++) {
                //duration represents the time between two marks
                info = "Name: " + performanceMeasure[i].name + " - " + "Duration Time: " + performanceMeasure[i].duration + "";
                console.log(info);
            }
            //delete mark1 identifier
            performance.clearMarks("mark1");
            //delete all marks
            performance.clearMarks();
            performance.clearMeasures("measure1");
            performance.clearMeasures();
        }
        else
        {
             document.getElementById("main_content").innerHTML = "User Timing API not supported";
        }
    }
    else
    {
        document.getElementById("main_content").innerHTML = "Page Performance API not supported";
    }
}