webtiming-resolution.any.js (847B)
1 function testTimeResolution(highResTimeFunc, funcString) { 2 test(() => { 3 const t0 = highResTimeFunc(); 4 let t1 = highResTimeFunc(); 5 while (t0 == t1) { 6 t1 = highResTimeFunc(); 7 } 8 const epsilon = 1e-5; 9 assert_greater_than_equal(t1 - t0, 0.005 - epsilon, 'The second ' + funcString + ' should be much greater than the first'); 10 }, 'Verifies the resolution of ' + funcString + ' is at least 5 microseconds.'); 11 } 12 13 function timeByPerformanceNow() { 14 return performance.now(); 15 } 16 17 function timeByUserTiming() { 18 performance.mark('timer'); 19 const time = performance.getEntriesByName('timer')[0].startTime; 20 performance.clearMarks('timer'); 21 return time; 22 } 23 24 testTimeResolution(timeByPerformanceNow, 'performance.now()'); 25 testTimeResolution(timeByUserTiming, 'entry.startTime');