JavaScript Reference ยท Browser and Web APIs
performance.now() and the Performance API
Reference for performance.now(), performance.mark(), measure(), PerformanceObserver and timeOrigin: measure code speed and page timings accurately.
performance.now() returns a high-resolution timestamp in milliseconds, measured from the moment the page (or worker) started. It is the right tool for timing code, because unlike Date.now() it never jumps when the system clock changes, and it has fractional milliseconds. Subtract two readings to get a duration.
The wider Performance API adds named markers and measurements. performance.mark(name) records a point in time, performance.measure(name, startMark, endMark) records the span between two marks, and a PerformanceObserver is told whenever the browser or your code adds a new entry. The same timeline holds the browser's own entries, such as navigation timing, resource loads and paint events.
const start = performance.now(); doWork(); console.log(performance.now() - start, "ms");
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
performance.now() performance.mark(name, options) performance.measure(name, startMarkOrOptions, endMark) performance.getEntries() performance.getEntriesByName(name, type) performance.getEntriesByType(type) performance.clearMarks(name) performance.clearMeasures(name) performance.timeOrigin new PerformanceObserver(callback).observe({ type: "measure", buffered: true })
| Name | Description |
|---|---|
name |
A string label for a mark or measure. |
startMark, endMark |
Names of earlier marks. If endMark is omitted, measure() ends now; if both are omitted, it starts at the time origin. |
options.detail |
Any structuredCloneable data attached to a mark or measure. |
options.start, options.end |
Timestamps or mark names for measure() when you use the options form. |
Return values: now() returns a number of milliseconds (a DOMHighResTimeStamp). mark() returns a PerformanceMark, measure() a PerformanceMeasure with name, startTime and duration. timeOrigin is the absolute start time in milliseconds since the Unix epoch.
Entry types include "mark", "measure", "navigation", "resource", "paint", "longtask", "largest-contentful-paint" and "layout-shift", depending on browser support.
Usage notes
- Use
performance.now()for durations,Date.now()for wall-clock times. Addperformance.timeOrigintonow()for an absolute timestamp. - Resolution is reduced on purpose. Browsers round timestamps, and may add jitter, to limit timing attacks. Do not expect nanosecond precision.
- Measure many runs. A single run includes warm-up, garbage collection and noise. Repeat the test and compare averages or medians.
- Name marks to match features, such as
"search-start"and"search-end", so they are readable in the DevTools Performance panel. - Clean up. Marks and measures stay in memory until cleared.
- Observers see entries as they are added; pass
buffered: true(withtype) to also receive earlier ones. - Real-user monitoring:
PerformanceObserverwith"navigation","paint"and"largest-contentful-paint"is how libraries report page speed to analytics. console.time()is a quick alternative that prints the elapsed time instead of returning it.
Browser support
performance.now(), marks and measures are supported in every current browser. Entry types like "longtask", "largest-contentful-paint" and "layout-shift" are available only in some, so check a compatibility table and feature-detect with PerformanceObserver.supportedEntryTypes.