How to Accurately Measure Load Times with the Performance.now() API

This guide explains how to use the browser's Performance.now() API to precisely measure the time between two markers, compare it with Date timestamps, and demonstrates a practical example of timing an image's load event with JavaScript code.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
How to Accurately Measure Load Times with the Performance.now() API

Performance API can measure the time between two predefined markers by defining a start and an end point.

Example: var start = performance.now(); The difference from using Date timestamps is twofold: performance.now() provides millisecond values with sub‑millisecond (three‑decimal) precision, whereas Date.now() returns integer milliseconds; and performance.now() measures time elapsed since the page navigation start, while Date gives the current epoch time.

Example: measuring an image's load time.

var start = performance.now();
document.getElementsByTagName("img")[0].addEventListener("load", function() {
    var end = performance.now();
    console.log((end - start) + "ms");
}, false);
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptfrontend developmentWeb Performanceload timingperformance.now
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.