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"),
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);
}
});

View File

@ -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++;