From 02019e9251070db58fda28c10391d6f7f5b87bd0 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sun, 14 Mar 2021 18:05:57 -0700 Subject: [PATCH] perf: use scheduling.isInputPending() (#1996) --- src/routes/_utils/scheduleIdleTask.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/routes/_utils/scheduleIdleTask.js b/src/routes/_utils/scheduleIdleTask.js index 1ee6a984..823a872a 100644 --- a/src/routes/_utils/scheduleIdleTask.js +++ b/src/routes/_utils/scheduleIdleTask.js @@ -15,9 +15,19 @@ function getRIC () { return typeof requestIdleCallback !== 'undefined' ? requestIdleCallback : liteRIC } +function getIsInputPending () { + return process.browser && navigator.scheduling && navigator.scheduling.isInputPending + ? () => navigator.scheduling.isInputPending() + : () => false // just assume input is not pending on browsers that don't support this +} + +const isInputPending = getIsInputPending() + function runTasks (deadline) { mark('scheduleIdleTask:runTasks()') - while (taskQueue.length && deadline.timeRemaining() > 0) { + // Bail out early if our deadline has passed (probably ~50ms) or if there is input pending + // See https://web.dev/isinputpending/ + while (taskQueue.length && deadline.timeRemaining() > 0 && !isInputPending()) { const task = taskQueue.shift() try { task()