DOM
Document Object Model
An API that represents HTML/XML documents as a tree structure. Manipulates web pages dynamically with JavaScript.
Document Object Model
An API that represents HTML/XML documents as a tree structure. Manipulates web pages dynamically with JavaScript.
The DOM (Document Object Model) is a programming interface that represents HTML and XML documents as an object model with a tree structure. Standardized by the W3C, it allows JavaScript to dynamically read and modify the structure, style, and content of a web page. When a browser parses HTML, a DOM tree is created.
A DOM tree is composed of nodes. There are node types such as Document, Element, Text, and Attribute, which are connected through parent-child-sibling relationships. Nodes are traversed with methods such as document.getElementById() and querySelector(), and manipulated with appendChild() and removeChild().
DOM manipulation is an expensive operation. When the DOM changes, the browser performs Reflow (layout recalculation) and Repaint (redrawing the screen). For this reason, frameworks such as React and Vue use a Virtual DOM, comparing changes in memory and then performing only the minimum necessary real DOM updates.
Events are also handled through the DOM. Events such as clicks and key presses propagate along the DOM tree by bubbling (from lower to upper) or capturing (from upper to lower). Using the Event Delegation pattern, you can handle events efficiently at the parent instead of attaching individual listeners to many child elements.
// DOM μμ νμ
const container = document.getElementById('app');
const buttons = document.querySelectorAll('.btn');
const firstCard = document.querySelector('.card:first-child');
// DOM μμ μμ± λ° μΆκ°
const newElement = document.createElement('div');
newElement.className = 'card';
newElement.innerHTML = `
<h3>μ μΉ΄λ</h3>
<p>λμ μΌλ‘ μμ±λ μ½ν
μΈ </p>
`;
container.appendChild(newElement);
// μ¬λ¬ μμ μΆκ° μ DocumentFragment μ¬μ© (μ±λ₯ μ΅μ ν)
const fragment = document.createDocumentFragment();
for (let i = 0; i < 100; i++) {
const li = document.createElement('li');
li.textContent = `μμ΄ν
${i + 1}`;
fragment.appendChild(li);
}
document.getElementById('list').appendChild(fragment);
// β DOM μ‘°μμ΄ ν λ²λ§ λ°μ (100λ² vs 1λ²)
// μ΄λ²€νΈ μμ (Event Delegation)
document.getElementById('todo-list').addEventListener('click', (e) => {
// ν΄λ¦λ μμκ° μμ λ²νΌμΈμ§ νμΈ
if (e.target.matches('.delete-btn')) {
const todoItem = e.target.closest('.todo-item');
todoItem.remove();
}
// 체ν¬λ°μ€ ν΄λ¦
if (e.target.matches('.todo-checkbox')) {
const todoItem = e.target.closest('.todo-item');
todoItem.classList.toggle('completed');
}
});
// μμ± λ° μ€νμΌ μ‘°μ
const card = document.querySelector('.card');
card.setAttribute('data-id', '123');
card.style.transform = 'translateY(-5px)';
card.classList.add('active');
card.classList.remove('hidden');
"We attached an event listener to every list item, but once it went past 1,000, memory usage spiked. If we attach just one listener to the parent ul and handle events with event delegation, the listeners drop from 1,000 to 1, which improves memory efficiency."
"Interviewer: Why is the Virtual DOM faster than the real DOM?
Candidate: The Virtual DOM itself isn't fast; it's fast because it minimizes the number of DOM manipulations. When state changes, it finds only the changes in the Virtual DOM using a diff algorithm and applies them to the real DOM as a batch update. One batch update causes less reflow/repaint than 10 individual DOM manipulations, so performance is better."
"You're calling appendChild 100 times inside a for loop. If you use a DocumentFragment, build the elements in memory first, and then attach them all at once, reflow drops from 100 times to 1. The perceived performance difference will be significant."
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free