Add some small performance improvements

* Only create the comment lookup map once
 * Only set the .removed and .deleted properties once
This commit is contained in:
Christopher Gurnee 2021-11-05 12:38:51 -04:00
parent aa7329889c
commit 0e36bc193d
2 changed files with 26 additions and 38 deletions

View file

@ -6,35 +6,20 @@ import {
showRemovedAndDeleted, showRemoved, showDeleted
} from '../../utils'
const arrayToLookup = (commentList, removed, deleted) => {
const lookup = new Map()
commentList.forEach(comment => {
comment.replies = []
if (removed.has(comment.id)) {
comment.removed = true
} else if (deleted.has(comment.id)) {
comment.deleted = true
}
lookup.set(comment.id, comment)
})
return lookup
}
const unflatten = (comments, root, removed, deleted) => {
const lookup = arrayToLookup(comments, removed, deleted)
const unflatten = (commentMap, root, removed, deleted) => {
const commentTree = []
lookup.forEach(comment => {
commentMap.forEach(comment => {
comment.replies = []
})
commentMap.forEach(comment => {
const parentID = comment.parent_id
let parentComment
if (parentID === root) {
commentTree.push(comment)
} else if ((parentComment = lookup.get(parentID)) !== undefined) {
} else if ((parentComment = commentMap.get(parentID)) !== undefined) {
parentComment.replies.push(comment)
} else {
console.error('MISSING PARENT ID:', parentID, 'for comment', comment)
@ -42,7 +27,7 @@ const unflatten = (comments, root, removed, deleted) => {
})
let rootComment
if ((rootComment = lookup.get(root)) !== undefined) {
if ((rootComment = commentMap.get(root)) !== undefined) {
rootComment.replies = commentTree
return [rootComment]
}

View file

@ -18,9 +18,9 @@ import CommentInfo from './CommentInfo'
class Thread extends React.Component {
state = {
post: {},
pushshiftComments: [],
removed: new Set(),
deleted: new Set(),
pushshiftCommentLookup: new Map(),
removed: [],
deleted: [],
loadingComments: true
}
@ -74,16 +74,17 @@ class Thread extends React.Component {
return getRedditComments(ids)
.then(redditComments => {
console.log(`Reddit: ${redditComments.length} comments`)
const removed = new Set()
const deleted = new Set()
const removed = []
const deleted = []
redditComments.forEach(comment => {
const pushshiftComment = pushshiftCommentLookup.get(comment.id)
let pushshiftComment = pushshiftCommentLookup.get(comment.id)
if (pushshiftComment === undefined) {
// When a parent comment is missing from pushshift, use the reddit comment instead
comment.parent_id = comment.parent_id.substring(3)
comment.link_id = comment.link_id.substring(3)
pushshiftCommentLookup.set(comment.id, comment)
pushshiftComment = comment
pushshiftCommentLookup.set(comment.id, pushshiftComment)
} else {
// Replace pushshift score with reddit (it's usually more accurate)
pushshiftComment.score = comment.score
@ -91,10 +92,12 @@ class Thread extends React.Component {
// Check what is removed / deleted according to reddit
if (isRemoved(comment.body)) {
removed.add(comment.id)
removed.push(comment.id)
pushshiftComment.removed = true
} else if (isDeleted(comment.body)) {
deleted.add(comment.id)
} else if (pushshiftComment !== undefined && isRemoved(pushshiftComment.body)) {
deleted.push(comment.id)
pushshiftComment.deleted = true
} else if (pushshiftComment !== comment && isRemoved(pushshiftComment.body)) {
// If it's deleted in pushshift, but later restored by a mod, use the restored
comment.parent_id = comment.parent_id.substring(3)
comment.link_id = comment.link_id.substring(3)
@ -104,7 +107,7 @@ class Thread extends React.Component {
this.props.global.setSuccess()
this.setState({
pushshiftComments: Array.from(pushshiftCommentLookup.values()),
pushshiftCommentLookup,
removed,
deleted,
loadingComments: false
@ -129,9 +132,9 @@ class Thread extends React.Component {
(!this.state.loadingComments && root) &&
<React.Fragment>
<CommentInfo
total={this.state.pushshiftComments.length}
removed={this.state.removed.size}
deleted={this.state.deleted.size}
total={this.state.pushshiftCommentLookup.size}
removed={this.state.removed.length}
deleted={this.state.deleted.length}
/>
<SortBy />
{isSingleComment &&
@ -142,7 +145,7 @@ class Thread extends React.Component {
}
<CommentSection
root={root}
comments={this.state.pushshiftComments}
comments={this.state.pushshiftCommentLookup}
removed={this.state.removed}
deleted={this.state.deleted}
/>