incremental-dom

An in-place DOM diffing library

README

CircleCI

Incremental DOM


Overview


Incremental DOM is a library for building up DOM trees and updating them in-place when data changes. It differs from the established virtual DOM approach in that no intermediate tree is created (the existing tree is mutated in-place). This approach significantly reduces memory allocation and GC thrashing for incremental updates to the DOM tree therefore increasing performance significantly in some cases.

Incremental DOM is primarily intended as a compilation target for templating languages. It could be used to implement a higher level API for human consumption. The API was carefully designed to minimize heap allocations and where unavoidable ensure that as many objects as possible can be de-allocated by incremental GC. One unique feature of its API is that it separates opening and closing of tags so that it is suitable as a compilation target for templating languages that allow (temporarily) unbalanced HTML in templates (e.g. tags that are opened and closed in separate templates) and arbitrary logic for creating HTML attributes.
Think of it as ASM.dom.

Supported Browsers


Incremental DOM supports IE9 and above.

Usage


HTML is expressed in Incremental DOM using the elementOpen, elementClose, elementVoid and text methods. Consider the following example:

  1. ``` js
  2. var IncrementalDOM = require('incremental-dom'),
  3.     elementOpen = IncrementalDOM.elementOpen,
  4.     elementClose = IncrementalDOM.elementClose,
  5.     elementVoid = IncrementalDOM.elementVoid,
  6.     text = IncrementalDOM.text;

  7. function render(data) {
  8.   elementVoid('input', '', [ 'type', 'text' ]);
  9.   elementOpen('div', '', null);
  10.     if (data.someCondition) {
  11.       text(data.text);
  12.     }
  13.   elementClose('div');
  14. }
  15. ```

To render or update an existing DOM node, the patch function is used:


  1. ``` js
  2. var patch = require('incremental-dom').patch;

  3. var data = {
  4.   text: 'Hello World!',
  5.   someCondition: true
  6. };

  7. patch(myElement, function() {
  8.   render(data);
  9. });

  10. data.text = 'Hello World!';

  11. patch(myElement, function() {
  12.   render(data);
  13. });
  14. ```

Templating Languages and Libraries


Check out  what others having been doing with Incremental DOM.

Docs



Getting Incremental DOM


Via CDN


https://ajax.googleapis.com/ajax/libs/incrementaldom/0.5.1/incremental-dom.js
https://ajax.googleapis.com/ajax/libs/incrementaldom/0.5.1/incremental-dom-min.js

Using npm


  1. ```sh
  2. npm install incremental-dom
  3. ```

Development


To install the required development packages, run the following command:

  1. ```sh
  2. npm i
  3. ```

Running tests


To run once:

  1. ```sh
  2. ./node_modules/.bin/bazelisk test ...
  3. ```

To run on change:

  1. ```sh
  2. ./node_modules/.bin/ibazel run //test:unit_tests
  3. ```

Building


To build once:

  1. ```sh
  2. ./node_modules/.bin/bazelisk build ...
  3. ```

To build on change:

  1. ```sh
  2. ./node_modules/.bin/ibazel build ...
  3. ```