aboutsummaryrefslogtreecommitdiff
path: root/node_modules/w3c-hr-time/lib/clock-is-accurate.js
diff options
context:
space:
mode:
authorJoel Kronqvist <work.joelkronqvist@pm.me>2022-03-11 20:46:06 +0200
committerJoel Kronqvist <work.joelkronqvist@pm.me>2022-03-11 20:46:06 +0200
commit080c5819d87b933816d724a83f3bf4f1686770a7 (patch)
tree4a2ccc68b27edf7d4cbc586c932cc7542b655e19 /node_modules/w3c-hr-time/lib/clock-is-accurate.js
parent5ac7049a9d30733165cc212dee308163c2a14644 (diff)
parentd003b82235a9329f912522a2f70aa950dfce4998 (diff)
downloadLYLLRuoka-080c5819d87b933816d724a83f3bf4f1686770a7.tar.gz
LYLLRuoka-080c5819d87b933816d724a83f3bf4f1686770a7.zip
Merge branch 'master' of https://github.com/JoelHMikael/FoodJS
Updating remote changes
Diffstat (limited to 'node_modules/w3c-hr-time/lib/clock-is-accurate.js')
-rw-r--r--node_modules/w3c-hr-time/lib/clock-is-accurate.js61
1 files changed, 61 insertions, 0 deletions
diff --git a/node_modules/w3c-hr-time/lib/clock-is-accurate.js b/node_modules/w3c-hr-time/lib/clock-is-accurate.js
new file mode 100644
index 0000000..920b499
--- /dev/null
+++ b/node_modules/w3c-hr-time/lib/clock-is-accurate.js
@@ -0,0 +1,61 @@
+"use strict";
+
+const { hrtime } = require("./utils");
+
+// The HR-TIME spec calls for 5-μs accuracy. Check that we have that in both hrtime() and Date.now().
+
+function testClockAccuracy() {
+ // Test hrtime() first. The check is simpler and more stable, and we use hrtime() to measure Date.now()'s performance.
+ const roundTrip = hrtime(hrtime());
+ if (roundTrip[0] > 1 || roundTrip[1] > 5e3 * 2) {
+ return false;
+ }
+
+ // Test Date.now() twice: first with a looser bound (10 μs) but with a smaller run time to filter out very bad
+ // Date.now() performance, and then with a tighter bound (5 μs) to check we have the accuracy we need.
+ let times;
+ // eslint-disable-next-line no-unused-vars
+ let cur;
+ let start;
+ let end;
+
+ times = 100;
+ start = hrtime();
+ while (times-- > 0) {
+ cur = Date.now();
+ }
+ end = hrtime(start);
+ if ((end[0] * 1e9 + end[1]) > 1000000) {
+ return false;
+ }
+
+ times = 10000;
+ start = hrtime();
+ while (times-- > 0) {
+ cur = Date.now();
+ }
+ end = hrtime(start);
+ if ((end[0] * 1e9 + end[1]) > 50000000) {
+ return false;
+ }
+
+ return true;
+}
+
+// Warm up the function.
+testClockAccuracy();
+testClockAccuracy();
+testClockAccuracy();
+
+const TIMES = 5;
+const THRESHOLD = 0.6 * TIMES;
+let accurates = 0;
+for (let i = 0; i < TIMES; i++) {
+ if (testClockAccuracy()) {
+ accurates++;
+ }
+}
+
+const isAccurate = accurates >= THRESHOLD;
+
+module.exports = isAccurate;