All Projects β†’ choojs β†’ Nanomorph

choojs / Nanomorph

Licence: mit
πŸš… - Hyper fast diffing algorithm for real DOM nodes

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Nanomorph

Differencekit
πŸ’» A fast and flexible O(n) difference algorithm framework for Swift collection.
Stars: ✭ 2,986 (+380.84%)
Mutual labels:  algorithm, diff
Diffabledatasources
πŸ’Ύ A library for backporting UITableView/UICollectionViewDiffableDataSource.
Stars: ✭ 601 (-3.22%)
Mutual labels:  algorithm, diff
Javascript Datastructures Algorithms
πŸ“š collection of JavaScript and TypeScript data structures and algorithms for education purposes. Source code bundle of JavaScript algorithms and data structures book
Stars: ✭ 3,221 (+418.68%)
Mutual labels:  algorithm, tree
lego
πŸš€ Web-components made lightweight & Future-Proof.
Stars: ✭ 69 (-88.89%)
Mutual labels:  virtual-dom, dom
Algorithms and data structures
180+ Algorithm & Data Structure Problems using C++
Stars: ✭ 4,667 (+651.53%)
Mutual labels:  algorithm, tree
object-dom
HTML Object Declarative Dom
Stars: ✭ 20 (-96.78%)
Mutual labels:  virtual-dom, dom
Editscript
A library designed to diff and patch Clojure data structures
Stars: ✭ 281 (-54.75%)
Mutual labels:  algorithm, diff
CalDOM
An agnostic, reactive & minimalist (3kb) JavaScript UI library with direct access to native DOM.
Stars: ✭ 176 (-71.66%)
Mutual labels:  virtual-dom, dom
Algodeck
An Open-Source Collection of 200+ Algorithmic Flash Cards to Help you Preparing your Algorithm & Data Structure Interview πŸ’―
Stars: ✭ 4,441 (+615.14%)
Mutual labels:  algorithm, tree
Java Algorithms Implementation
Algorithms and Data Structures implemented in Java
Stars: ✭ 3,927 (+532.37%)
Mutual labels:  algorithm, tree
AdvancedHTMLParser
Fast Indexed python HTML parser which builds a DOM node tree, providing common getElementsBy* functions for scraping, testing, modification, and formatting. Also XPath.
Stars: ✭ 90 (-85.51%)
Mutual labels:  tree, dom
Treelib
An efficient implementation of tree data structure in python 2/3.
Stars: ✭ 540 (-13.04%)
Mutual labels:  algorithm, tree
js-symbol-tree
Turn any collection of objects into its own efficient tree or linked list using Symbol
Stars: ✭ 86 (-86.15%)
Mutual labels:  tree, dom
gitree
Print a directory tree that shows Git status and ignores files dictated by .gitignore.
Stars: ✭ 32 (-94.85%)
Mutual labels:  diff, tree
respo.cljs
A virtual DOM library built with ClojureScript, inspired by React and Reagent.
Stars: ✭ 232 (-62.64%)
Mutual labels:  virtual-dom, dom
Data Structures Algorithms
My implementation of 85+ popular data structures and algorithms and interview questions in Python 3 and C++
Stars: ✭ 273 (-56.04%)
Mutual labels:  algorithm, tree
Rrt Algorithms
n-dimensional RRT, RRT* (RRT-Star)
Stars: ✭ 195 (-68.6%)
Mutual labels:  algorithm, tree
Swiftlcs
Swift implementation of the longest common subsequence (LCS) algorithm.
Stars: ✭ 207 (-66.67%)
Mutual labels:  algorithm, diff
Algorithms
Minimal examples of data structures and algorithms in Python
Stars: ✭ 20,123 (+3140.42%)
Mutual labels:  algorithm, tree
Algorithms
CLRS study. Codes are written with golang.
Stars: ✭ 482 (-22.38%)
Mutual labels:  algorithm, tree

nanomorph stability

npm version build status downloads js-standard-style

Hyper fast diffing algorithm for real DOM nodes ⚑️

Usage

var morph = require('nanomorph')
var html = require('nanohtml')

var tree = html`<div>hello people</div>`
document.body.appendChild(tree)
// document.body === <body><div>hello people</div></body>

morph(tree, html`<div>nanananana-na-no</div>`)
// document.body === <body><div>nanananana-na-no</div></body>

morph(tree, html`<div>teeny, tiny, tin bottle</div>`)
// document.body === <body><div>teeny, tiny, tin bottle</div></body>

Clearing Input Values

To remove values from inputs, there's a few options:

html`<input class="beep" value=${null}>` // set the value to null
html`<input class="beep">`               // omit property all together

Reordering Lists

It's common to work with lists of elements on the DOM. Adding, removing or reordering elements in a list can be rather expensive. To optimize this you can add an id attribute to a DOM node. When reordering nodes it will compare nodes with the same ID against each other, resulting in far fewer re-renders. This is especially potent when coupled with DOM node caching.

var el = html`
  <section>
    <div id="first">hello</div>
    <div id="second">world</div>
  </section>
`

Caching DOM elements

Sometimes we want to tell the algorithm to not evaluate certain nodes (and its children). This can be because we're sure they haven't changed, or perhaps because another piece of code is managing that part of the DOM tree. To achieve this nanomorph evaluates the .isSameNode() method on nodes to determine if they should be updated or not.

var el = html`<div>node</div>`

// tell nanomorph to not compare the DOM tree if they're both divs
el.isSameNode = function (target) {
  return (target && target.nodeName && target.nodeName === 'DIV')
}

Prevent Morphing Particular Elements

There are situations where two elements should never be morphed, but replaced. nanomorph automatically does this for elements with different tag names. But if we're implementing a custom component system, for example, components of different types should probably be treated as if they had different tagsβ€”even if they both render a <div> at their top level.

Nodes can have an optional data-nanomorph-component-id attribute. nanomorph will only ever morph nodes if they both have the same value in this attribute. If the values differ, the old node is replaced with the new one.

var el = html`<div data-nanomorph-component-id="a">hello</div>`
var el2 = html`<div data-nanomorph-component-id="b">goodbye</div>`

assert.equal(nanomorph(el, el2), el2)

nanomorph doesn't have an opinion on the values of the data-nanomorph-component-id attribute, so we can decide the meaning we give it on a case by case basis. There could be a unique ID for every type of component, or a unique ID for every instance of a component, or any other meaning.

FAQ

How is this different from morphdom?

It's quite similar actually; the API of this library is completely compatible with morphdom and we've borrowed a fair few bits. The main difference is that we copy event handlers like onclick, don't support browsers that are over a decade old, and don't provide custom behavior by removing all hooks. This way we can guarantee a consistent, out-of-the box experience for all your diffing needs.

Why doesn't this work in Node?

Node has no concept of a DOM - server side rendering is basically fancy string concatenation. If you want to combine HTML strings in Node, check out hyperstream.

This library seems cool, I'd like to build my own!

Nanomorph was optimized for simplicity, but different situations might require different tradeoffs. So in order to allow folks to build their own implementation we expose our test suite as a function you can call. So regardless if you're doing it to solve a problem, or just for fun: you can use the same tests we use for your own implementation. Yay! ✨

API

tree = nanomorph(oldTree, newTree)

Diff a tree of HTML elements against another tree of HTML elements and create a patched result that can be applied on the DOM.

⚠️ nanomorph will modify the newTree and it should be discarded after use

Installation

$ npm install nanomorph

See Also

Similar Packages

Further Reading

Authors

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].