🌐 Web Development

DOM

Document Object Model

An API that represents HTML/XML documents as a tree structure. Manipulates web pages dynamically with JavaScript.

πŸ“– Detailed Explanation

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.

πŸ’» Code Example

// 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');

πŸ—£οΈ Examples of Practical Conversations

πŸ’Ό In a Performance Optimization Meeting

"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."

🎯 In a Technical Interview

"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."

πŸ” In a Code Review

"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

⚠️ Cautions

πŸ”— Related Terms

πŸ“š Learn More