From 598ab03fd070f4c9e2569abbe439d983bff79746 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Thu, 14 Dec 2023 10:40:22 +0000 Subject: [PATCH 1/2] Optimise getUniqueByProp --- packages/server/src/db/linkedRows/linkUtils.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/server/src/db/linkedRows/linkUtils.ts b/packages/server/src/db/linkedRows/linkUtils.ts index 5942e7e5a1..0d2e99fc6c 100644 --- a/packages/server/src/db/linkedRows/linkUtils.ts +++ b/packages/server/src/db/linkedRows/linkUtils.ts @@ -99,9 +99,15 @@ export async function getLinkDocuments(args: { } export function getUniqueByProp(array: any[], prop: string) { - return array.filter((obj, pos, arr) => { - return arr.map(mapObj => mapObj[prop]).indexOf(obj[prop]) === pos - }) + const seen = new Set() + const ret = [] + for (const item of array) { + if (!seen.has(item[prop])) { + seen.add(item[prop]) + ret.push(item) + } + } + return ret } export function getLinkedTableIDs(table: Table): string[] { From 0597c0efc6246a2a2388ce9235a42f939d550253 Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Thu, 14 Dec 2023 11:08:55 +0000 Subject: [PATCH 2/2] Respond to PR feedback. --- packages/server/src/db/linkedRows/linkUtils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/server/src/db/linkedRows/linkUtils.ts b/packages/server/src/db/linkedRows/linkUtils.ts index 0d2e99fc6c..faaf9c1f21 100644 --- a/packages/server/src/db/linkedRows/linkUtils.ts +++ b/packages/server/src/db/linkedRows/linkUtils.ts @@ -100,14 +100,14 @@ export async function getLinkDocuments(args: { export function getUniqueByProp(array: any[], prop: string) { const seen = new Set() - const ret = [] + const filteredArray = [] for (const item of array) { if (!seen.has(item[prop])) { seen.add(item[prop]) - ret.push(item) + filteredArray.push(item) } } - return ret + return filteredArray } export function getLinkedTableIDs(table: Table): string[] {