A platform for community discussion. Free, open, simple.
PERF: Optimize `Category.subcategories` (#31802)
David Taylorc307e5aa47d6
diff --git a/app/assets/javascripts/discourse/app/models/category.js b/app/assets/javascripts/discourse/app/models/category.jsindex ae83ed5f9d8..ee6b8f80879 100644--- a/app/assets/javascripts/discourse/app/models/category.js+++ b/app/assets/javascripts/discourse/app/models/category.js@@ -503,9 +503,9 @@ export default class Category extends RestModel {this.set("parent_category_id", newParentCategory?.id);}- @computed("site.categories.[]")+ @computed("site.categoriesByParentId")get subcategories() {- return this.site.categories.filterBy("parent_category_id", this.id);+ return this.site.categoriesByParentId.get(this.id) || [];}@computed("subcategories")diff --git a/app/assets/javascripts/discourse/app/models/site.js b/app/assets/javascripts/discourse/app/models/site.jsindex 76049e81cd8..a83d90cd274 100644--- a/app/assets/javascripts/discourse/app/models/site.js+++ b/app/assets/javascripts/discourse/app/models/site.js@@ -147,6 +147,17 @@ export default class Site extends RestModel {return map;}+ @computed("[email protected]_category_id")+ get categoriesByParentId() {+ const map = new Map();+ for (const category of this.categories) {+ const siblings = map.get(category.parent_category_id) || [];+ siblings.push(category);+ map.set(category.parent_category_id, siblings);+ }+ return map;+ }+@discourseComputed("notification_types")notificationLookup(notificationTypes) {const result = [];