From 8683d4e85df887a86c3811c4acde62c324a9c3ec Mon Sep 17 00:00:00 2001 From: Sainan <63328889+Sainan@users.noreply.github.com> Date: Mon, 30 Jun 2025 10:34:34 +0200 Subject: [PATCH] chore: optimise creditsController Doing both lookups in parallel saves around 1 ms in the happy case (20% of baseline time), and in case nonce does not match, the error is simply raised as per usual with the inventory request being lightweight enough to be negligible. Noteworthy that this reasoning doesn't really work for other controllers because in the error case, the inventory request would still be quite significant, even if the HTTP request itself would still finish quickly. --- src/controllers/api/creditsController.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/controllers/api/creditsController.ts b/src/controllers/api/creditsController.ts index 5fb60575..d7b91d24 100644 --- a/src/controllers/api/creditsController.ts +++ b/src/controllers/api/creditsController.ts @@ -4,9 +4,15 @@ import { getAccountIdForRequest } from "@/src/services/loginService"; import { getInventory } from "@/src/services/inventoryService"; export const creditsController: RequestHandler = async (req, res) => { - const accountId = await getAccountIdForRequest(req); - - const inventory = await getInventory(accountId, "RegularCredits TradesRemaining PremiumCreditsFree PremiumCredits"); + const inventory = ( + await Promise.all([ + getAccountIdForRequest(req), + getInventory( + req.query.accountId as string, + "RegularCredits TradesRemaining PremiumCreditsFree PremiumCredits" + ) + ]) + )[1]; const response = { RegularCredits: inventory.RegularCredits,