Use monotonic timer
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 23 Apr 2023 00:21:55 +0000 (02:21 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 23 Apr 2023 00:21:55 +0000 (02:21 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
busy-wait.js

index 66d58967ed9e86c0cf00b61708379dc46f2e0be4..9c392a53c06755d1e1d3f321f9c6ae176d14d3b5 100644 (file)
@@ -8,9 +8,9 @@ const interval = 1000
  * @param timeoutMs
  */
 function dummyTimeoutBusyWait (timeoutMs) {
-  const timeoutTimestampMs = Date.now() + timeoutMs
+  const timeoutTimestampMs = performance.now() + timeoutMs
   // eslint-disable-next-line no-empty
-  do {} while (Date.now() < timeoutTimestampMs)
+  do {} while (performance.now() < timeoutTimestampMs)
 }
 
 /**
@@ -18,10 +18,10 @@ function dummyTimeoutBusyWait (timeoutMs) {
  * @param intervalMs
  */
 async function sleepTimeoutBusyWait (timeoutMs, intervalMs = interval) {
-  const timeoutTimestampMs = Date.now() + timeoutMs
+  const timeoutTimestampMs = performance.now() + timeoutMs
   do {
     await sleep(intervalMs)
-  } while (Date.now() < timeoutTimestampMs)
+  } while (performance.now() < timeoutTimestampMs)
 }
 
 /**