Skip to content

Instantly share code, notes, and snippets.

@deepal
Last active March 15, 2022 20:44
Show Gist options
  • Save deepal/871151b803badaef4ad614795e09c2df to your computer and use it in GitHub Desktop.
Save deepal/871151b803badaef4ad614795e09c2df to your computer and use it in GitHub Desktop.
setFakeTime();
const startTimeToD = Date.now(); // set startTime with time-of-day clock
const startTimeMon = performance.now(); // set startTime with monotonic clock
setImmediate(() => {
correctTimeNTP(); // synchronise the clock
});
await doSomething();
const endTimeToD = Date.now(); // set endTime with time-of-day clock
const endTimeMon = performance.now(); // set endTime with monotonic clock
const durationToD = endTimeToD - startTimeToD;
const durationMon = endTimeMon - startTimeMon;
const error = (durationToD - durationMon) / 1000;
console.log(`Duration measured by time-of-day clock\t: ${durationToD}ms`);
console.log(`Duration measured by monotonic clock\t: ${durationMon}ms`);
console.log(`Error: ${error.toFixed(3)}s`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment