Created
July 7, 2017 19:03
-
-
Save ngryman/69717b9b6eb57da2a50a9faad02fb004 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Having a constructor tells the VM that this object as a given "shape". | |
| * If this shape is constant, all the code manipulating it will eventually | |
| * be optimized into specialized stubs. This has a HUGE performance impact | |
| * and that's why sometimes JavaScript can be so close to the metal. | |
| */ | |
| function VNode(tag, data, children) { | |
| this.tag = tag | |
| this.data = data | |
| this.children = children || [] | |
| } | |
| /** | |
| * This holds trashed vnodes that are not used anymore. | |
| * Wee keep them here to be able to re-use them later. As we keep a reference | |
| * to them here, GC won't try to collect them. No collection = GOOD. | |
| */ | |
| const nodePool = [] | |
| const setNode = (vnode, tag, data, children) => { | |
| vnode.tag = tag | |
| vnode.data = data | |
| vnode.children = children | |
| return vnode | |
| } | |
| /** | |
| * This first try to take a trashed vnode and recycle it (assing new properties | |
| * to it). The advatage of doing so is that it's a cheap operation, no allocation | |
| * is happening here, only assignements. | |
| */ | |
| const recycleNode = (tag, data, children) => ( | |
| nodePool.length > 0 | |
| ? setNode(nodePool.pop(), tag, data, children) | |
| : new VNode(tag, data, children) | |
| ) | |
| const collectNode = (vnode) => { | |
| nodePool.push(vnode) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment