Dev.Lib
arrow_backBack to Blogs
LEVEL_077.9June 27, 20261 min read

MutationObserver cost

Web Platform APIs

Observes DOM mutations — expensive on large subtrees and high churn.

Summary

Observes DOM mutations — expensive on large subtrees and high churn. Prefer framework lifecycle or targeted observers. subtree: true on document.body is a performance footgun. Use for third-party widget integration when no hooks exist.

Code example

const mo = new MutationObserver((mutations) => {
  for (const m of mutations) {
    if (m.type === 'childList') reconcileThirdPartyWidget(m.target);
  }
});
mo.observe(widgetRoot, { childList: true, subtree: false });

Key points

  • Observes DOM mutations — expensive on large subtrees and high churn.
  • Prefer framework lifecycle or targeted observers.
  • subtree: true on document.body is a performance footgun.
  • Use for third-party widget integration when no hooks exist.

Further reading

Related concepts