From 89681977b5fe96643dc2671685ec3f9f51ef9ce8 Mon Sep 17 00:00:00 2001 From: baiyongjie <407221377@qq.com> Date: Sun, 23 Apr 2023 15:36:34 +0800 Subject: [PATCH 1/2] perf: improve descendant count performance --- src/routes/api/note_map.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/routes/api/note_map.js b/src/routes/api/note_map.js index b8487e6ce..9f33960d2 100644 --- a/src/routes/api/note_map.js +++ b/src/routes/api/note_map.js @@ -3,9 +3,12 @@ const becca = require("../../becca/becca"); const { JSDOM } = require("jsdom"); const NotFoundError = require("../../errors/not_found_error"); +function buildDescendantCountMap(noteIdsToCount) { + if (!Array.isArray(noteIdsToCount)) { + throw new Error('noteIdsToCount: type error'); + } -function buildDescendantCountMap() { - const noteIdToCountMap = {}; + const noteIdToCountMap = Object.create(null); function getCount(noteId) { if (!(noteId in noteIdToCountMap)) { @@ -24,12 +27,12 @@ function buildDescendantCountMap() { return noteIdToCountMap[noteId]; } - - getCount('root'); + noteIdsToCount.forEach((nodeId) => { + getCount(nodeId); + }); return noteIdToCountMap; } - /** * @param {BNote} note * @param {int} depth @@ -119,7 +122,9 @@ function getLinkMap(req) { noteIds.add(noteId); } - const notes = Array.from(noteIds).map(noteId => { + const noteIdsArray = Array.from(noteIds) + + const notes = noteIdsArray.map(noteId => { const note = becca.getNote(noteId); return [ @@ -155,7 +160,7 @@ function getLinkMap(req) { return { notes: notes, - noteIdToDescendantCountMap: buildDescendantCountMap(), + noteIdToDescendantCountMap: buildDescendantCountMap(noteIdsArray), links: links }; } @@ -209,7 +214,7 @@ function getTreeMap(req) { }); } - const noteIdToDescendantCountMap = buildDescendantCountMap(); + const noteIdToDescendantCountMap = buildDescendantCountMap(Array.from(noteIds)); updateDescendantCountMapForSearch(noteIdToDescendantCountMap, subtree.relationships); From e007aba06702a3630549c9892b157651014141e2 Mon Sep 17 00:00:00 2001 From: Castor <407221377@qq.com> Date: Mon, 24 Apr 2023 13:43:19 +0800 Subject: [PATCH 2/2] Update note_map.js --- src/routes/api/note_map.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/routes/api/note_map.js b/src/routes/api/note_map.js index 9f33960d2..11fc9a3e6 100644 --- a/src/routes/api/note_map.js +++ b/src/routes/api/note_map.js @@ -27,8 +27,8 @@ function buildDescendantCountMap(noteIdsToCount) { return noteIdToCountMap[noteId]; } - noteIdsToCount.forEach((nodeId) => { - getCount(nodeId); + noteIdsToCount.forEach((noteId) => { + getCount(noteId); }); return noteIdToCountMap;