Measure elapsed time with performance.now()
Quick answer
Use performance.now() for elapsed-time measurements within one execution environment. Its millisecond reading is relative to a time origin and uses a monotonic clock rather than the adjustable civil-time clock.
How it works
Record const start = performance.now() before work and subtract it from the next reading. The difference is a duration in milliseconds; precision can be reduced for privacy, and consecutive readings may be equal. performance.timeOrigin supplies the timestamp corresponding to the origin; do not directly subtract readings from unrelated contexts. Date.now() is useful for wall-clock timestamps, not for assuming an uninterrupted monotonic duration. Measure a real workload repeatedly and report the environment and distribution.
Local examples
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const start=performance.now(); const values=[1,2,3].map(n=>n*2); const elapsed=performance.now()-start; console.log(values.join(","),elapsed>=0);})()Expected console output:
2,4,6 trueThe transformation is deterministic; only the nonnegative duration relation is asserted.
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {console.log(Number.isFinite(performance.timeOrigin),typeof performance.now());})()Expected console output:
true numberThe timestamp is finite; the exact clock value is not predicted.
Common mistakes
Do not assert a positive duration for very small work or compare a raw performance.now() reading to Date.now().
Practice
Measure a bounded array transformation ten times; report the minimum and median, not a universal speed claim.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Measure elapsed time with performance.now() — Knowledge Check
Apply the concept and reason about local example output.
Which clock is appropriate for measuring elapsed work within one page?
Why can two successive performance.now readings be equal?
What should be reported with repeated performance measurements?
What is the exact console output in Measure elapsed time with performance.now() example 1?
(async () => {const start=performance.now(); const values=[1,2,3].map(n=>n*2); const elapsed=performance.now()-start; console.log(values.join(","),elapsed>=0);})()What is the exact console output in Measure elapsed time with performance.now() example 2?
(async () => {console.log(Number.isFinite(performance.timeOrigin),typeof performance.now());})()