Skip to content

Instantly share code, notes, and snippets.

@ThisIsMissEm
Forked from jed/bookmarklet.js
Created April 28, 2011 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThisIsMissEm/946045 to your computer and use it in GitHub Desktop.
Save ThisIsMissEm/946045 to your computer and use it in GitHub Desktop.
dom tree caching performance: array v. linked list
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( "Node1: " + node1.count(), Date.now() - t );
t = Date.now()
node2 = new Node2( document );
console.log( "Node2: " + node2.count(), Date.now() - t );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment