From cdb8a43f76ad8e7d43f7a8a18e3574c82c00bb69 Mon Sep 17 00:00:00 2001 From: Wyatt Allen Date: Wed, 4 Apr 2018 22:48:39 -0700 Subject: [PATCH] More efficient thread grouping MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A nested loop made ChangeComments#getCommentThreads O(n²) in the worst case. Use a map to convert it to O(n). Change-Id: I39f6a462cdd23316a1b513ebf98bbd2c8f9aaec2 --- .../app/elements/diff/gr-comment-api/gr-comment-api.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/polygerrit-ui/app/elements/diff/gr-comment-api/gr-comment-api.js b/polygerrit-ui/app/elements/diff/gr-comment-api/gr-comment-api.js index 065219ae3b..4b64f7b4e4 100644 --- a/polygerrit-ui/app/elements/diff/gr-comment-api/gr-comment-api.js +++ b/polygerrit-ui/app/elements/diff/gr-comment-api/gr-comment-api.js @@ -395,14 +395,15 @@ */ ChangeComments.prototype.getCommentThreads = function(comments) { const threads = []; + const idThreadMap = {}; for (const comment of comments) { // If the comment is in reply to another comment, find that comment's // thread and append to it. if (comment.in_reply_to) { - const thread = threads.find(thread => - thread.comments.some(c => c.id === comment.in_reply_to)); + const thread = idThreadMap[comment.in_reply_to]; if (thread) { thread.comments.push(comment); + idThreadMap[comment.id] = thread; continue; } } @@ -419,6 +420,7 @@ newThread.commentSide = comment.side; } threads.push(newThread); + idThreadMap[comment.id] = newThread; } return threads; };