Question presented to candidate: "If you wanted to measure how long a function takes to run, why would you reach for performance.now() instead of just calling Date.now() before and after it?"
What a strong answer should cover:
- performance.now() returns a high-resolution timestamp, expressed as a float with sub-millisecond precision, relative to a fixed time origin -- not an absolute epoch value.
- Date.now() returns whole milliseconds since the Unix epoch and is tied to the system wall clock, which can be adjusted (NTP sync, manual changes) while a measurement is in progress.
- performance.now() is implemented as a monotonic clock -- it never goes backward, so it stays safe for measuring elapsed durations even if the wall clock is adjusted mid-measurement, unlike Date.now().
- For a genuinely fast operation, Date.now() can report a 0ms delta simply because the operation finished inside the same millisecond tick, while performance.now() can report real, non-zero sub-millisecond timing for that same operation.
- Real caveat worth naming: browsers deliberately coarsen and jitter performance.now()'s resolution as a Spectre-era security mitigation, so its resolution is not unlimited in a browser the way it can appear to be in a Node.js process.
Clarifying questions expected:
- "Is this for browser code, Node.js code, or both -- the security-driven resolution coarsening only applies inside browsers."
Code / implementation expected: Yes -- a runnable snippet comparing both clocks' resolution and timing the same fast operation with each.