Skip to content

Commit a9fb72d

Browse files
authored
perf(test): optimize test-timer checkIfTimeIsUp (@fehmer) (monkeytypegame#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.
1 parent ea14499 commit a9fb72d

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

frontend/src/ts/test/test-timer.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -153,29 +153,25 @@ function checkIfFailed(
153153

154154
function checkIfTimeIsUp(): void {
155155
if (timerDebug) console.time("times up check");
156-
if (
157-
Config.mode === "time" ||
158-
(Config.mode === "custom" && CustomText.getLimitMode() === "time")
159-
) {
160-
if (
161-
(Time.get() >= Config.time &&
162-
Config.time !== 0 &&
163-
Config.mode === "time") ||
164-
(Time.get() >= CustomText.getLimitValue() &&
165-
CustomText.getLimitValue() !== 0 &&
166-
Config.mode === "custom")
167-
) {
168-
//times up
169-
if (timer !== null) clearTimeout(timer);
170-
Caret.hide();
171-
TestInput.input.pushHistory();
172-
TestInput.corrected.pushHistory();
173-
SlowTimer.clear();
174-
slowTimerCount = 0;
175-
TimerEvent.dispatch("finish");
176-
return;
177-
}
156+
let maxTime = undefined;
157+
158+
if (Config.mode === "time") {
159+
maxTime = Config.time;
160+
} else if (Config.mode === "custom" && CustomText.getLimitMode() === "time") {
161+
maxTime = CustomText.getLimitValue();
162+
}
163+
if (maxTime !== undefined && maxTime !== 0 && Time.get() >= maxTime) {
164+
//times up
165+
if (timer !== null) clearTimeout(timer);
166+
Caret.hide();
167+
TestInput.input.pushHistory();
168+
TestInput.corrected.pushHistory();
169+
SlowTimer.clear();
170+
slowTimerCount = 0;
171+
TimerEvent.dispatch("finish");
172+
return;
178173
}
174+
179175
if (timerDebug) console.timeEnd("times up check");
180176
}
181177

0 commit comments

Comments
 (0)