From d9ab5e81cf6a030438b36e0c27d45f20317c316e Mon Sep 17 00:00:00 2001 From: Yoonseok Ko Date: Thu, 3 Nov 2022 05:19:36 -0700 Subject: [PATCH] A bug fix - the undefined behavior of `idleCallbacks`'s `timeRemaining`. Summary: ## Changelog: [Android] [Fixed] - Fix a bug that returns a random number from callback argument `timeRemaining` of `idleCallbacks` registered by `requestIdleCallbacks`. `global.performance.now()` returns a time from an unknown origin, while `frameTime` is epoch time. Thus, `global.performance.now() - frameTime` is just a random number rather than an elapsed time from the frame start time. As a simple solution, I would suggest to use `Date.now()`. Its resolution is lower than `frameTime` due to the security issue in JavaScript VM. So, it loses some precision, but, at least, it is not wrong. Reviewed By: javache Differential Revision: D40941461 fbshipit-source-id: b0094e181b97a844d133a9268fe48cd8c8fadfda --- Libraries/Core/Timers/JSTimers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Libraries/Core/Timers/JSTimers.js b/Libraries/Core/Timers/JSTimers.js index 33f8a68d76b67f..f996f9c26d47da 100644 --- a/Libraries/Core/Timers/JSTimers.js +++ b/Libraries/Core/Timers/JSTimers.js @@ -379,7 +379,7 @@ const JSTimers = { callIdleCallbacks: function (frameTime: number) { if ( - FRAME_DURATION - (global.performance.now() - frameTime) < + FRAME_DURATION - (Date.now() - frameTime) < IDLE_CALLBACK_FRAME_DEADLINE ) { return;