From b730dc7983e7bf98aaf0d89b17a30cd47b5cc4e1 Mon Sep 17 00:00:00 2001 From: Waseem Daher Date: Tue, 16 Oct 2012 16:45:01 -0400 Subject: [PATCH] Huge speedup in get_next_visible/get_prev_visible. nextAll/prevAll walks the entire DOM, basically. This code only walks the DOM until we find a new .message_row. This speeds up the average time of a call to this function from about 6.38ms to 0.678ms, in my benchmarking. Admittedly, the whole outer loop here could still use some optimization, if we want to; do we really need to call this 1000 times? (imported from commit 852e2f660a16f8cfd7be35d3271aedb1ac481663) --- zephyr/static/js/dom_access.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/zephyr/static/js/dom_access.js b/zephyr/static/js/dom_access.js index 49ddf073ae..91d41b97e7 100644 --- a/zephyr/static/js/dom_access.js +++ b/zephyr/static/js/dom_access.js @@ -1,13 +1,18 @@ +// We need to andSelf() because more often than not, the next item +// *is* a .message_row, so nextUntil returns an empty set (except for +// when it's in a bookend). +// (This could probably be further optimized by handling that case +// explicitly first, since it's also the common case.) function get_next_visible(message_row) { if (message_row === undefined) return []; - return message_row.nextAll('.message_row').filter(':first'); + return message_row.nextUntil('.message_row').andSelf().next('.message_row'); } function get_prev_visible(message_row) { if (message_row === undefined) return []; - return message_row.prevAll('.message_row').filter(':first'); + return message_row.prevUntil('.message_row').andSelf().prev('.message_row'); } function get_first_visible() {