From 7dd32aba2382b21582d0e2d07f885d0566f2b86a Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Sat, 10 Dec 2011 18:52:09 +0100 Subject: [PATCH] Append all the new posts at once, using a documentFragment. Optimizations. --- 4chan_x.user.js | 25 +++++++++++++------------ script.coffee | 22 +++++++++++----------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 3419f1262..f744a3abc 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -2012,7 +2012,7 @@ } }, update: function() { - var arr, body, id, input, replies, reply, scroll, _i, _len, _ref, _ref2; + var body, frag, id, input, newPosts, reply, scroll, _i, _j, _len, _len2, _ref, _ref2, _ref3; if (this.status === 404) { updater.timer.textContent = ''; updater.count.textContent = 404; @@ -2038,24 +2038,25 @@ updater.count.className = 'error'; return; } - replies = $$('.reply', body); - id = Number(((_ref2 = $('td[id]', updater.br.previousElementSibling)) != null ? _ref2.id : void 0) || 0); - arr = []; - while ((reply = replies.pop()) && (reply.id > id)) { - arr.push(reply.parentNode.parentNode.parentNode); + id = ((_ref2 = $('td[id]', updater.br.previousElementSibling)) != null ? _ref2.id : void 0) || 0; + frag = d.createDocumentFragment(); + _ref3 = $$('.reply', body).reverse(); + for (_j = 0, _len2 = _ref3.length; _j < _len2; _j++) { + reply = _ref3[_j]; + if (reply.id === id) break; + $.prepend(frag, reply.parentNode.parentNode.parentNode); } - scroll = conf['Scrolling'] && updater.focus && arr.length && (d.body.scrollHeight - d.body.clientHeight - window.scrollY < 20); + newPosts = frag.childNodes.length; + scroll = conf['Scrolling'] && updater.focus && newPosts && (d.body.scrollHeight - d.body.clientHeight - window.scrollY < 20); if (conf['Verbose']) { - updater.count.textContent = '+' + arr.length; - if (arr.length === 0) { + updater.count.textContent = '+' + newPosts; + if (newPosts === 0) { updater.count.className = ''; } else { updater.count.className = 'new'; } } - while (reply = arr.pop()) { - $.before(updater.br, reply); - } + $.before(updater.br, frag); if (scroll) return scrollTo(0, d.body.scrollHeight); } }, diff --git a/script.coffee b/script.coffee index 3500da8c9..12301289b 100644 --- a/script.coffee +++ b/script.coffee @@ -1625,23 +1625,23 @@ updater = updater.count.className = 'error' return - replies = $$ '.reply', body - id = Number $('td[id]', updater.br.previousElementSibling)?.id or 0 - arr = [] - while (reply = replies.pop()) and (reply.id > id) - arr.push reply.parentNode.parentNode.parentNode #table + id = $('td[id]', updater.br.previousElementSibling)?.id or 0 + frag = d.createDocumentFragment() + for reply in $$('.reply', body).reverse() + if reply.id is id + break + $.prepend frag, reply.parentNode.parentNode.parentNode #table - scroll = conf['Scrolling'] && updater.focus && arr.length && (d.body.scrollHeight - d.body.clientHeight - window.scrollY < 20) + newPosts = frag.childNodes.length + scroll = conf['Scrolling'] && updater.focus && newPosts && (d.body.scrollHeight - d.body.clientHeight - window.scrollY < 20) if conf['Verbose'] - updater.count.textContent = '+' + arr.length - if arr.length is 0 + updater.count.textContent = '+' + newPosts + if newPosts is 0 updater.count.className = '' else updater.count.className = 'new' - #XXX add replies in correct order so backlinks resolve - while reply = arr.pop() - $.before updater.br, reply + $.before updater.br, frag if scroll scrollTo 0, d.body.scrollHeight