Created
April 28, 2011 08:57
-
-
Save jed/946036 to your computer and use it in GitHub Desktop.
dom tree caching performance: array v. linked list. see also: http://jsperf.com/dom-trees/
This file contains 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
javascript:with(document)(body.appendChild(createElement('script')).src='https://gist.github.com/raw/946036/8b51f04a6f9dbe15f5c375ac872a9ae56d4b8732/domTrees.js')._ |
This file contains 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
var t, count, node1, node2 | |
function Node1(node) { | |
this.node = node; | |
var children = node.childNodes, | |
i = this.length = children.length; | |
while (i--) this[i] = new Node1(children[i]); | |
} | |
Node1.prototype.count = function() { | |
var ret = 1, i = this.length; | |
while ( i-- ) ret += this[ i ].count(); | |
return ret; | |
} | |
function Node2(node) { | |
var first = node.firstChild, | |
next = node.nextSibling; | |
this.node = node; | |
this.first = first && new Node2(first); | |
this.next = next && new Node2(next); | |
} | |
Node2.prototype.count = function() { | |
var ret = 1; | |
if ( this.first ) ret += this.first.count(); | |
if ( this.next ) ret += this.next.count(); | |
return ret; | |
} | |
t = Date.now() | |
node1 = new Node1( document ); | |
console.log( "time to create (Node1)", Date.now() - t ) | |
t = Date.now() | |
count = node1.count() | |
console.log( "time to count (Node1)", Date.now() - t ) | |
console.log( "nodes counted (Node1)", count ) | |
t = Date.now() | |
node2 = new Node2( document ); | |
console.log( "time to create (Node2)", Date.now() - t ) | |
t = Date.now() | |
count = node2.count() | |
console.log( "time to count (Node2)", Date.now() - t ) | |
console.log( "nodes counted (Node2)", count ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment