source: frontend/node_modules/w3c-hr-time/lib/clock-is-accurate.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 1.4 KB
Line 
1"use strict";
2
3const { hrtime } = require("./utils");
4
5// The HR-TIME spec calls for 5-μs accuracy. Check that we have that in both hrtime() and Date.now().
6
7function testClockAccuracy() {
8 // Test hrtime() first. The check is simpler and more stable, and we use hrtime() to measure Date.now()'s performance.
9 const roundTrip = hrtime(hrtime());
10 if (roundTrip[0] > 1 || roundTrip[1] > 5e3 * 2) {
11 return false;
12 }
13
14 // Test Date.now() twice: first with a looser bound (10 μs) but with a smaller run time to filter out very bad
15 // Date.now() performance, and then with a tighter bound (5 μs) to check we have the accuracy we need.
16 let times;
17 // eslint-disable-next-line no-unused-vars
18 let cur;
19 let start;
20 let end;
21
22 times = 100;
23 start = hrtime();
24 while (times-- > 0) {
25 cur = Date.now();
26 }
27 end = hrtime(start);
28 if ((end[0] * 1e9 + end[1]) > 1000000) {
29 return false;
30 }
31
32 times = 10000;
33 start = hrtime();
34 while (times-- > 0) {
35 cur = Date.now();
36 }
37 end = hrtime(start);
38 if ((end[0] * 1e9 + end[1]) > 50000000) {
39 return false;
40 }
41
42 return true;
43}
44
45// Warm up the function.
46testClockAccuracy();
47testClockAccuracy();
48testClockAccuracy();
49
50const TIMES = 5;
51const THRESHOLD = 0.6 * TIMES;
52let accurates = 0;
53for (let i = 0; i < TIMES; i++) {
54 if (testClockAccuracy()) {
55 accurates++;
56 }
57}
58
59const isAccurate = accurates >= THRESHOLD;
60
61module.exports = isAccurate;
Note: See TracBrowser for help on using the repository browser.