From 64aaff1f3dcb2f648342c7d8e90e6a23d229aefc Mon Sep 17 00:00:00 2001 From: Namekuji Date: Tue, 22 Aug 2023 09:13:49 -0400 Subject: [PATCH] refactor: use set instead of array --- .../src/server/api/endpoints/hashtags/trend.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/hashtags/trend.ts b/packages/backend/src/server/api/endpoints/hashtags/trend.ts index 0d23b47659..4a581ebd39 100644 --- a/packages/backend/src/server/api/endpoints/hashtags/trend.ts +++ b/packages/backend/src/server/api/endpoints/hashtags/trend.ts @@ -82,7 +82,7 @@ export default define(meta, paramDef, async () => { const tags: { name: string; - users: Note["userId"][]; + users: Set; }[] = []; for (const [_, fields] of tagNotes) { @@ -91,16 +91,16 @@ export default define(meta, paramDef, async () => { if (hiddenTags.includes(name)) continue; const index = tags.findIndex((tag) => tag.name === name); - if (index >= 0 && !tags[index].users.includes(userId)) { - tags[index].users.push(userId); - } else if (index < 0) { - tags.push({ name, users: [userId] }); + if (index >= 0) { + tags[index].users.add(userId); + } else { + tags.push({ name, users: new Set([userId]) }); } } // Sort tags by their popularity const hots = tags - .sort((a, b) => b.users.length - a.users.length) + .sort((a, b) => b.users.size - a.users.size) .map((tag) => tag.name) .slice(0, max); @@ -108,7 +108,7 @@ export default define(meta, paramDef, async () => { const stats = hots.map((tag, i) => ({ tag, chart: [], // FIXME: generate chart - usersCount: tags[i].users.length, + usersCount: tags[i].users.size, })); return stats;