From 7cb576a53549c8851da3ac9d34d473a128753314 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Tue, 26 Sep 2023 18:47:33 +0200 Subject: [PATCH] Greatly improve id conversion performance --- packages/backend/src/misc/convert-id.ts | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/packages/backend/src/misc/convert-id.ts b/packages/backend/src/misc/convert-id.ts index 5458ae657..c82a9b86a 100644 --- a/packages/backend/src/misc/convert-id.ts +++ b/packages/backend/src/misc/convert-id.ts @@ -9,27 +9,19 @@ const chars = '0123456789abcdefghijklmnopqrstuvwxyz'; //FIXME: Make this idempotent export function convertId(id: string, target: IdType): string { if (target == IdType.IceshrimpId) { - let input = BigInt(id); - let result = ''; - - while (input !== 0n) { - result = chars.at(Number(input % 36n)) + result; - input /= 36n; - } - - return result; + return BigInt(id).toString(36); } else if (target == IdType.MastodonId) { let result = 0n; - const iter = id.toLowerCase().split('').reverse(); + const iter = id.toLowerCase(); for (let i = 0; i < iter.length; i++){ const char = iter[i]; if (!chars.includes(char)) throw new Error('Invalid ID'); - result += BigInt(chars.indexOf(char)) * BigInt(36 ** i); + result = result * 36n + BigInt(chars.indexOf(char)); } return result.toString(); } - throw new Error('Unknown ID type'); + throw new Error('Unknown ID type'); }