Skip to content

Instantly share code, notes, and snippets.

@jed
Created April 28, 2011 08:57
Show Gist options
  • Save jed/946036 to your computer and use it in GitHub Desktop.
Save jed/946036 to your computer and use it in GitHub Desktop.

Revisions

  1. jed revised this gist Apr 28, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bookmarklet.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    javascript:with(document)(body.appendChild(createElement('script')).src='https://gist.github.com/raw/946036/0dcc583f962f850282a88d201430ee6eb3e7e158/domTrees.js')._
    javascript:with(document)(body.appendChild(createElement('script')).src='https://gist.github.com/raw/946036/8b51f04a6f9dbe15f5c375ac872a9ae56d4b8732/domTrees.js')._
  2. jed revised this gist Apr 28, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion domTrees.js
    Original file line number Diff line number Diff line change
    @@ -46,7 +46,7 @@ console.log( "nodes counted (Node1)", count )

    t = Date.now()
    node2 = new Node2( document );
    console.log( "time to create (Node)", Date.now() - t )
    console.log( "time to create (Node2)", Date.now() - t )

    t = Date.now()
    count = node2.count()
  3. jed revised this gist Apr 28, 2011. 1 changed file with 14 additions and 2 deletions.
    16 changes: 14 additions & 2 deletions domTrees.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    var t, count, node1, node2

    function Node1(node) {
    this.node = node;

    @@ -35,8 +37,18 @@ Node2.prototype.count = function() {

    t = Date.now()
    node1 = new Node1( document );
    console.log( "Node1: " + node1.count(), Date.now() - t );
    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( "Node2: " + node2.count(), Date.now() - t );
    console.log( "time to create (Node)", Date.now() - t )

    t = Date.now()
    count = node2.count()
    console.log( "time to count (Node2)", Date.now() - t )
    console.log( "nodes counted (Node2)", count )
  4. jed revised this gist Apr 28, 2011. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions bookmarklet.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    javascript:with(document)(body.appendChild(createElement('script')).src='https://gist.github.com/raw/946036/0dcc583f962f850282a88d201430ee6eb3e7e158/domTrees.js')._
  5. jed created this gist Apr 28, 2011.
    42 changes: 42 additions & 0 deletions domTrees.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    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 );