The letter S in a light blue, stylized speech bubble followed by SpeakBits
SpeakBitsThe letter S in a light blue, stylized speech bubble followed by SpeakBits
Trending
Top
New
Controversial
Search
Groups

Enjoying SpeakBits?

Support the development of it by donating to Patreon or Ko-Fi.
About
Rules
Terms
Privacy
EULA
Cookies
Blog
Have feedback? We'd love to hear it!

Patterns for Memory Efficient DOM Manipulation with Modern Vanilla JavaScript

frontendmasters.com
submitted
10 mos ago
byjosephtoprogramming

Summary

When you render HTML, the live view of those rendered elements in the browser is called the DOM. There is an entire set of APIs specifically dealing with modifying this tree of elements. These are attached to the document, so you use them like const el = document.querySelector("#el"); They are also available on all other elements, so if you have an element reference you can use these methods.

Visual Studio Code is written in vanilla JavaScript “to be as close to the DOM as possible” This article explains how to keep your DOM unchanged by hiding and showing elements instead of destroying and creating them with JavaScript.

The insertAdjacentHTML method is much faster than innerText because it doesn’t have to destroy the DOM first before inserting. The innerText method is cool because it is aware of the current styles of an element. If you want an element both visually hidden and hidden to assistive technology, display: none; should do it.

When you remove a DOM node, you don’t want references sitting around that prevent the garbage collector from cleaning up associated data. You can associate data to DOM nodes using WeakMap.

Using matches(selector) only matches the current element, so it needs to be the leaf node. You can use the Abort controller to remove sets of events.

Memory profiling can help you identify potential leaks or unnecessary allocations during DOM manipulation. The Performance tab is invaluable for analyzing JavaScript execution time, which is crucial when optimizing DOM manipulation code.

Understanding and applying these low-level techniques can significantly boost your app's performance. Excessive manipulation, even with efficient methods, can still lead to performance issues. The goal isn’t always to forgo frameworks and manually manipulate the DOM for every project. Rather, it’s to understand these principles so you can make informed decisions about when to use frameworks and when to optimize at a lower level.

 web site website internet site site cash machine cash dispenser automated teller machine automatic teller machine automated teller automatic teller ATM analog clock menu-0
8

7 Comments

2
kaiserseahorse
10 mos ago
I think that, for all its faults, this is the kind of thing that react and jsx shines at
2
josephOP
10 mos ago
You still have to deal with all that possible overhead instead of the direct control
1
kaiserseahorse
9 mos ago
DX is amazing though! Doing vanilla javascript is always just awkward enough.
2
josephOP
9 mos ago
I believe there's a way to get JSX without the rest of react
1
kaiserseahorse
9 mos ago
Got any more info on this?
2
josephOP
9 mos ago
Astro lets you be JS framework agnostic. There's also NakedJSX, which Chris Coyier went into a little here
2
kaiserseahorse
9 mos ago*
Thanks for this! Going to need to try some of these out. Been seeing Astro around but never gave it a shot.