From a9fb72de7cd4a36c81cd4c076a1fd78fdae253ba Mon Sep 17 00:00:00 2001 From: Christian Fehmer Date: Mon, 26 May 2025 16:01:44 +0200 Subject: [PATCH] perf(test): optimize test-timer checkIfTimeIsUp (@fehmer) (#6588) During e.g. time 60 tests the CustomText.getLimitValue was called each second which is not needed. Refactor the checkIfTimeIsUp to only use the correct limit, either Config.time if mode is time or CustomText.getLimitValue if mode is custom and limited by time. --- frontend/src/ts/test/test-timer.ts | 40 ++++++++++++++---------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/frontend/src/ts/test/test-timer.ts b/frontend/src/ts/test/test-timer.ts index ce6972582..4d5c0d7b7 100644 --- a/frontend/src/ts/test/test-timer.ts +++ b/frontend/src/ts/test/test-timer.ts @@ -153,29 +153,25 @@ function checkIfFailed( function checkIfTimeIsUp(): void { if (timerDebug) console.time("times up check"); - if ( - Config.mode === "time" || - (Config.mode === "custom" && CustomText.getLimitMode() === "time") - ) { - if ( - (Time.get() >= Config.time && - Config.time !== 0 && - Config.mode === "time") || - (Time.get() >= CustomText.getLimitValue() && - CustomText.getLimitValue() !== 0 && - Config.mode === "custom") - ) { - //times up - if (timer !== null) clearTimeout(timer); - Caret.hide(); - TestInput.input.pushHistory(); - TestInput.corrected.pushHistory(); - SlowTimer.clear(); - slowTimerCount = 0; - TimerEvent.dispatch("finish"); - return; - } + let maxTime = undefined; + + if (Config.mode === "time") { + maxTime = Config.time; + } else if (Config.mode === "custom" && CustomText.getLimitMode() === "time") { + maxTime = CustomText.getLimitValue(); } + if (maxTime !== undefined && maxTime !== 0 && Time.get() >= maxTime) { + //times up + if (timer !== null) clearTimeout(timer); + Caret.hide(); + TestInput.input.pushHistory(); + TestInput.corrected.pushHistory(); + SlowTimer.clear(); + slowTimerCount = 0; + TimerEvent.dispatch("finish"); + return; + } + if (timerDebug) console.timeEnd("times up check"); }