From 08941025bf560c20e1ed87240f227c4bb779b3da Mon Sep 17 00:00:00 2001 From: Charmander <~@charmander.me> Date: Wed, 18 Dec 2024 17:46:59 -0800 Subject: [PATCH] cleanup: some unnecessary uses of `childNodes` a pointless microoptimization informed only by a reading of Blink source of questionable accuracy but the resulting code is also cleaner --- assets/js/scripts.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/assets/js/scripts.js b/assets/js/scripts.js index c42d46a0f..76d562ab7 100644 --- a/assets/js/scripts.js +++ b/assets/js/scripts.js @@ -19,6 +19,14 @@ return false; } + var empty = containerNode => { + var child; + + while (child = containerNode.firstChild) { + containerNode.removeChild(child); + } + }; + var hasModifierKeys = e => e.ctrlKey || e.shiftKey || e.altKey || e.metaKey; @@ -396,7 +404,7 @@ } if (!isBody && allowedTags.indexOf(node.nodeName.toLowerCase()) === -1) { - while (node.childNodes.length) { + while (node.hasChildNodes()) { node.parentNode.insertBefore(node.firstChild, node); } @@ -633,7 +641,7 @@ weasylMarkdown(fragment); defang(fragment, true); - while (fragment.childNodes.length) { + while (fragment.hasChildNodes()) { container.appendChild(fragment.firstChild); } } @@ -641,11 +649,7 @@ function updateMarkdownPreview(input) { if (markedLoadState === 2) { var preview = input.nextSibling; - - while (preview.childNodes.length) { - preview.removeChild(preview.firstChild); - } - + empty(preview); renderMarkdown(input.value, preview); } else { loadMarked(); @@ -837,10 +841,9 @@ target.textContent = 'Reply'; target.removeEventListener('click', cancelReply); - if (children.childNodes.length === 1) { + children.removeChild(newListItem); + if (!children.hasChildNodes()) { children.parentNode.removeChild(children); - } else { - children.removeChild(newListItem); } target.focus();