Performance API (performance.mark & measure)
Performance API (performance.mark & measure)
While console.time() and Date.now() are useful for rudimentary logging, real enterprise performance instrumentation requires sub-millisecond precision and integration with browser performance profiling tools. The User Timing API (performance.mark() and performance.measure()) is the W3C standard for benchmarking code execution.
1. High-Resolution Time: performance.now()
Date.now() returns time in whole milliseconds and is subject to system clock adjustments (NTP syncs).
In contrast, performance.now() returns a floating-point number representing milliseconds with microsecond resolution (accurate to thousandths of a millisecond), measured monotonically from page creation:
2. Using performance.mark() and performance.measure()
The User Timing API stores timestamped markers in the browser's high-speed Performance Timeline buffer:
3. Visualizing in DevTools Performance Panel
When you run a profile in the DevTools Performance panel, any performance.mark() and performance.measure() entries appear automatically in the Timings flame-chart lane, providing visual context alongside network requests and layout frames!
4. Collecting Measures with PerformanceObserver
Rather than polling, use a PerformanceObserver to stream performance measurements asynchronously to analytics servers:
5. Cleaning Up Performance Markers
Clear the internal browser buffer to prevent memory buildup:
Practice Quiz
Logging Architecture & Structured Log Levels
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Web Audio & MediaStream APIs Basics | Logging Architecture & Structured Log Levels |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.