Lightweight module for morphing an existing DOM node tree to match a target DOM node tree. It's fast and works with the real DOM—no virtual DOM needed!
This module was created to solve the problem of updating the DOM in response to a UI component or page being rerendered. One way to update the DOM is to simply toss away the existing DOM tree and replace it with a new DOM tree (e.g.,
myContainer.innerHTML = newHTML). While replacing an existing DOM tree with an entirely new DOM tree will actually be very fast, it comes with a cost. The cost is that all of the internal state associated with the existing DOM nodes (scroll positions, input caret positions, CSS transition states, etc.) will be lost. Instead of replacing the existing DOM tree with a new DOM tree we want to transform the existing DOM tree to match the new DOM tree while minimizing the number of changes to the existing DOM tree. This is exactly what the
morphdom module does! Give it an existing DOM node tree and a target DOM node tree and it will efficiently transform the existing DOM node tree to exactly match the target DOM node tree with the minimum amount of changes.
morphdom does not rely on any virtual DOM abstractions. Because
morphdom is using the real DOM, the DOM that the web browser is maintaining will always be the source of truth. Even if you have code that manually manipulates the DOM things will still work as expected. In addition,
morphdom can be used with any templating language that produces an HTML string.
The transformation is done in a single pass of both the original DOM tree and the target DOM tree and is designed to minimize changes to the DOM while still ensuring that the morphed DOM exactly matches the target DOM. In addition, the algorithm used by this module will automatically match up elements that have corresponding IDs and that are found in both the original and target DOM tree.
Support for diffing the real DOM with a virtual DOM was introduced in
v2.1.0. Virtual DOM nodes are expected to implement the minimal subset of the real DOM API required by
morphdom and virtual DOM nodes are automatically upgraded real DOM nodes if they need to be moved into the real DOM. For more details, please see: docs/virtual-dom.md.
First install the module into your project:
npm install morphdom --save
NOTE: There is also a UMD version of this module in the published npm package:
The code below shows how to morph one
<div> element to another
var morphdom = ;var el1 = document;el1className = 'foo';var el2 = document;el2className = 'bar';;to;
You can also pass in an HTML string for the second argument:
var morphdom = ;var el1 = document;el1className = 'foo';el1innerHTML = 'Hello John';;to;to;
NOTE: This module will modify both the original and target DOM node tree during the transformation. It is assumed that the target DOM node tree will be discarded after the original DOM node tree is morphed.
- IE7+ and any modern browser
- Proper namespace support added in
morphdom(fromNode, toNode, options) : Node
morphdom(fromNode, toNode, options) function supports the following arguments:
- fromNode (
Node)- The node to morph
- toNode (
String) - The node that the
fromNodeshould be morphed to (or an HTML string)
- options (
Object) - See below for supported options
The returned value will typically be the
fromNode. However, in situations where the
fromNode is not compatible with the
toNode (either different node type or different tag name) then a different DOM node will be returned.
Supported options (all optional):
- getNodeKey (
Function(node)) - Called to get the
Node's unique identifier. This is used by
morphdomto rearrange elements rather than creating and destroying an element that already exists. This defaults to using the
- onBeforeNodeAdded (
Function(node)) - Called before a
totree is added to the
fromtree. If this function returns
falsethen the node will not be added. Should return the node to be added.
- onNodeAdded (
Function(node)) - Called after a
totree has been added to the
- onBeforeElUpdated (
Function(fromEl, toEl)) - Called before a
fromtree is updated. If this function returns
falsethen the element will not be updated.
- onElUpdated (
Function(el)) - Called after a
fromtree has been updated.
- onBeforeNodeDiscarded (
Function(node)) - Called before a
fromtree is discarded. If this function returns
falsethen the node will not be discarded.
- onNodeDiscarded (
Function(node)) - Called after a
fromtree has been discarded.
- onBeforeElChildrenUpdated (
Function(fromEl, toEl)) - Called before the children of a
fromtree are updated. If this function returns
falsethen the child nodes will not be updated.
- childrenOnly (
Boolean) - If
truethen only the children of the
toNodenodes will be morphed (the containing element will be skipped). Defaults to
var morphdom = ;var morphedNode =;
Isn't the DOM slow?
No, the DOM data structure is not slow. The DOM is a key part of any web browser so it must be fast. Walking a DOM tree and reading the attributes on DOM nodes is not slow. However, if you attempt to read a computed property on a DOM node that requires a relayout of the page then that will be slow. However,
morphdom only cares about the following properties and methods of a DOM node:
actualize(document)(non-standard, used to upgrade a virtual DOM node to a real DOM node)
What about the virtual DOM?
Libraries such as a React and virtual-dom solve a similar problem using a Virtual DOM. That is, at any given time there will be the real DOM (that the browser rendered) and a lightweight and persistent virtual DOM tree that is a mirror of the real DOM tree. Whenever the view needs to update, a new virtual DOM tree is rendered. The new virtual DOM tree is then compared with the old virtual DOM tree using a diffing algorithm. Based on the differences that are found, the real DOM is then "patched" to match the new virtual DOM tree and the new virtual DOM tree is persisted for future diffing.
morphdom and virtual DOM based solutions update the real DOM with the minimum number of changes. The only difference is in how the differences are determined.
morphdom compares real DOM nodes while
virtual-dom and others only compare virtual DOM nodes.
There are some drawbacks to using a virtual DOM-based approach:
- The real DOM is not the source of truth (the persistent virtual DOM tree is the source of truth)
- The real DOM cannot be modified behind the scenes (e.g., no jQuery) because the diff is done against the virtual DOM tree
- A copy of the real DOM must be maintained in memory at all times (albeit a lightweight copy of the real DOM)
- The virtual DOM is an abstraction layer that introduces code overhead
- The virtual DOM representations are not standardized and will vary by implementation
- The virtual DOM can only efficiently be used with code and templating languages that produce a virtual DOM tree
The premise for using a virtual DOM is that the DOM is "slow". While there is slightly more overhead in creating actual DOM nodes instead of lightweight virtual DOM nodes, we are not seeing any noticeable slowness in our benchmarks. In addition, as web browsers get faster the DOM data structure will also likely continue to get faster so there benefits to avoiding the abstraction layer.
UPDATE: As of
morphdom supports both diffing a real DOM tree with another real DOM tree and diffing a real DOM tree with a virtual DOM tree. See: docs/virtual-dom.md for more details.
Which is better: rendering to an HTML string or rendering virtual DOM nodes?
There are many high performance templating engines that stream out HTML strings with no intermediate virtual DOM nodes being produced. On the server, rendering directly to an HTML string will always be faster than rendering virtual DOM nodes (that then get serialized to an HTML string). In a benchmark where we compared server-side rendering for Marko (with Marko Widgets) and React we found that Marko was able to render pages ten times faster than React with much lower CPU usage (see: Marko vs React: Performance Benchmark)
A good strategy to optimize for performance is to render a template to an HTML string on the server, but to compile the template such that it renders to a DOM/virtual DOM in the browser. This approach offers the best performance for both the server and the browser. In the near future, support for rendering to a virtual DOM will be added to the Marko templating engine.
What projects are using
morphdom is being used in the following projects:
- Omi.js (
v1.0.1+) - Open and modern framework for building user interfaces.
- Marko Widgets (
v5.0.0-beta+) - Marko Widgets is a high performance and lightweight UI components framework that uses the Marko templating engine for rendering UI components. You can see how Marko Widgets compares to React in performance by taking a look at the following benchmark: Marko vs React: Performance Benchmark
- Catberry.js (
v6.0.0+) - Catberry is a framework with Flux architecture, isomorphic web-components and progressive rendering.
- Composer.js (
v1.2.1) - Composer is a set of stackable libraries for building complex single-page apps. It uses morphdom in its rendering engine for efficient and non-destructive updates to the DOM.
- yo-yo.js (
v1.2.2) - A tiny library for building modular UI components using DOM diffing and ES6 tagged template literals. yo-yo powers a tiny, isomorphic framework called choo (
v3.3.0), which is designed to be fun.
- vomit.js (
v0.9.19) - A library that uses the power of ES6 template literals to quickly create DOM elements that you can update and compose with Objects, Arrays, other DOM elements, Functions, Promises and even Streams. All with the ease of a function call.
NOTE: If you are using a
morphdom in your project please send a PR to add your project here
Below are the results on running benchmarks on various DOM transformations for both
morphdom and virtual-dom. This benchmark uses a high performance timer (i.e.,
window.performance.now()) if available. For each test the benchmark runner will run
100 iterations. After all of the iterations are completed for one test the average time per iteration is calculated by dividing the total time by the number of iterations.
To run the benchmarks:
npm run benchmark
The table below shows some sample benchmark results when running the benchmarks on a MacBook Pro (2.8 GHz Intel Core i7, 16 GB 1600 MHz DDR3). The average time per iteration for each test is shown in the table below:
- Total time for morphdom: 359.02ms (winner)
- Total time for virtual-dom: 438.30ms
NOTE: Safari Version 9.1.1 (11601.6.17)
Pull Requests welcome. Please submit Github issues for any feature enhancements, bugs or documentation problems. Please make sure tests pass: