DEV: Use Set instead of array-as-an-object (#14511)

DEV: Use Set instead of array-as-an-object (#14511)

I don’t know if JS engines were able to optimize-away those gigantic arrays but in any case that’s a definite anti-pattern.

diff --git a/app/assets/javascripts/discourse/app/models/topic-list.js b/app/assets/javascripts/discourse/app/models/topic-list.js
index da291e8..b019da2 100644
--- a/app/assets/javascripts/discourse/app/models/topic-list.js
+++ b/app/assets/javascripts/discourse/app/models/topic-list.js
@@ -43,12 +43,11 @@ const TopicList = RestModel.extend({
   canLoadMore: notEmpty("more_topics_url"),
 
   forEachNew(topics, callback) {
-    const topicIds = [];
-
-    this.topics.forEach((topic) => (topicIds[topic.id] = true));
+    const topicIds = new Set();
+    this.topics.forEach((topic) => topicIds.add(topic.id));
 
     topics.forEach((topic) => {
-      if (!topicIds[topic.id]) {
+      if (!topicIds.has(topic.id)) {
         callback(topic);
       }
     });
diff --git a/app/assets/javascripts/discourse/app/routes/discovery-categories.js b/app/assets/javascripts/discourse/app/routes/discovery-categories.js
index aeb2351..344a3be 100644
--- a/app/assets/javascripts/discourse/app/routes/discovery-categories.js
+++ b/app/assets/javascripts/discourse/app/routes/discovery-categories.js
@@ -53,13 +53,12 @@ const DiscoveryCategoriesRoute = DiscourseRoute.extend(OpenComposer, {
       const url = `${getURL("/")}latest.json?topic_ids=${topic_ids.join(",")}`;
 
       return ajax({ url, data: this.params }).then((result) => {
-        const topicIds = [];
-
-        this.topics.forEach((topic) => (topicIds[topic.id] = true));
+        const topicIds = new Set();
+        this.topics.forEach((topic) => topicIds.add(topic.id));
 
         let i = 0;
         TopicList.topicsFrom(store, result).forEach((topic) => {
-          if (!topicIds[topic.id]) {
+          if (!topicIds.has(topic.id)) {
             topic.set("highlight", true);
             this.topics.insertAt(i, topic);
             i++;

GitHub sha: d3a59e3f695c467c22d02db62532194e17ac827b

This commit appears in #14511 which was approved by OsamaSayegh. It was merged by CvX.

Merge approval