2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-06 10:50:21 +08:00

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.
This commit is contained in:
Jarek Radosz 2021-10-05 14:35:32 +02:00 committed by GitHub
parent cd8a608d17
commit d3a59e3f69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 8 deletions

View file

@ -43,12 +43,11 @@ const TopicList = RestModel.extend({
canLoadMore: notEmpty("more_topics_url"), canLoadMore: notEmpty("more_topics_url"),
forEachNew(topics, callback) { forEachNew(topics, callback) {
const topicIds = []; const topicIds = new Set();
this.topics.forEach((topic) => topicIds.add(topic.id));
this.topics.forEach((topic) => (topicIds[topic.id] = true));
topics.forEach((topic) => { topics.forEach((topic) => {
if (!topicIds[topic.id]) { if (!topicIds.has(topic.id)) {
callback(topic); callback(topic);
} }
}); });

View file

@ -53,13 +53,12 @@ const DiscoveryCategoriesRoute = DiscourseRoute.extend(OpenComposer, {
const url = `${getURL("/")}latest.json?topic_ids=${topic_ids.join(",")}`; const url = `${getURL("/")}latest.json?topic_ids=${topic_ids.join(",")}`;
return ajax({ url, data: this.params }).then((result) => { return ajax({ url, data: this.params }).then((result) => {
const topicIds = []; const topicIds = new Set();
this.topics.forEach((topic) => topicIds.add(topic.id));
this.topics.forEach((topic) => (topicIds[topic.id] = true));
let i = 0; let i = 0;
TopicList.topicsFrom(store, result).forEach((topic) => { TopicList.topicsFrom(store, result).forEach((topic) => {
if (!topicIds[topic.id]) { if (!topicIds.has(topic.id)) {
topic.set("highlight", true); topic.set("highlight", true);
this.topics.insertAt(i, topic); this.topics.insertAt(i, topic);
i++; i++;