跪拜 Guibai
← All articles
Frontend · JavaScript · Web Components

Lit: A 5KB Web Component Library That Skips Virtual DOM Diffing

By 泯泷 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

A component built on Lit works in React, Vue, or plain HTML without an adapter, because it is a native custom element. That interoperability, plus a 5 KB core and precise DOM updates, makes it a credible alternative for teams that want framework-agnostic UI without surrendering developer experience.

Summary

Lit sits as a thin layer on top of the browser's native Web Components spec, promising the productivity of a framework without the bundle size. Its core is about 5 KB after minification and it handles reactive state, declarative templates through lit-html, and scoped styles via Shadow DOM. The library's standout technical decision is to bypass the virtual DOM altogether: using JavaScript tagged template literals, it parses each template once and marks the exact positions of dynamic expressions, then updates only those specific DOM nodes when state changes. That cuts out the overhead of building and diffing a shadow tree against the real DOM.

The standards-based foundation means a Lit component is a real custom element, inheriting from HTMLElement, and runs in any HTML context. That makes it a practical choice for shared component libraries and design systems that need to work across React, Vue, or no framework at all. LitElement, the base class, removes the boilerplate of manually syncing properties and attributes, providing reactive properties, declarative templates, and scoped styles out of the box.

The comparison to VDOM frameworks is deliberate. React and Vue maintain an in-memory copy of the DOM and compute diffs on every state change; Lit instead treats updates as surgical operations on the specific dynamic parts. The article argues this gives Lit an edge in update performance and memory usage, and it positions Lit as a more platform-aligned development model.

Takeaways
— Lit's minified core is roughly 5 KB.
— Lit components are standard custom elements inheriting from HTMLElement.
— Lit uses Shadow DOM to scope styles by default.
— The LitElement base class offers reactive properties, declarative templates via lit-html, and scoped styles.
— Lit parses templates once with tagged template literals and updates only the dynamic 'parts' directly, avoiding virtual DOM diffing.
— Web Components consist of Custom Elements, Shadow DOM, and HTML Templates.
Conclusions

The claim that Lit's precise updates usually beat VDOM diffing in performance and memory is a generalisation; the actual advantage depends on the size and frequency of state changes, and VDOM frameworks like React batch updates for large trees.

Building on Web Components means Lit gives up some conveniences of framework ecosystems, such as React's state management or Vue's reactivity system, which may be a trade-off for teams already invested in those tools.

Lit's 'future-ready' promise rests on browser standards being slower to change than framework abstractions; if a new DOM API arrives, standard components get it automatically, but frameworks often require migration.

Concepts & terms
Web Components
A set of browser-native APIs for creating reusable, encapsulated custom HTML elements, comprising Custom Elements, Shadow DOM, and HTML Templates.
Shadow DOM
A DOM encapsulation mechanism that isolates an element's internal structure and styles from the main document, preventing selector collisions.
Tagged Template Literals
A JavaScript feature where a template literal is preceded by a tag function, allowing the library to parse the raw string and interpolated expressions for efficient template processing.
Virtual DOM
An in-memory representation of the UI that a framework diffs against the real DOM to compute minimal updates, used by React and Vue.
LitElement
The base class in Lit that extends HTMLElement and provides reactive properties, declarative templates, and scoped styles.
From the discussion
Featured comments
组合搭配函数

Lit's approach is spot on - rather than inventing a new framework, it stands directly on the Web Components standard, which makes cross-framework component reuse click into place immediately. At 5KB and only touching the changed parts, the lightweight angle is quite appealing.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗