Linked List vs Array Traversal

JavaScript performance comparison

Revision 2 of this test case created by Jim Cowart

Preparation code

<script src="http://underscorejs.org/underscore.js"></script>
<script src="https://raw.github.com/postaljs/postal.js/master/lib/postal.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var LinkedNode = function(item, prev, next) {
      this.item = item;
      this.prev = prev;
      this.next = next;
    };
   
    var LinkedList = function(item) {
      this.head = item ? new LinkedNode(item, null, null) : null;
      this.last = this.head;
      this.length = 0;
      this.current;
    };
   
    LinkedList.prototype.add = function( item ) {
      var node;
      if( this.head === null ) {
        node = this.head = new LinkedNode(item, null, null);
      } else {
        node = this.last.next = new LinkedNode(item, this.last, null);
      }
      this.length += 1;
      this.last = node;
    };
     
    var NO_OP = function() {};
    var list = new LinkedList();
    var arry = [];
    var i = 200
    while(i > 0) {
      list.add(postal.subscribe({ channel: "channel" + i, topic: "topic." + i, callback: NO_OP }));
      arry.push(postal.subscribe({ channel: "channel" + i, topic: "topic." + i, callback: NO_OP }));
      i--;
    }
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Linked List Traversal
for(var x = list.head; x; x = x.next) {
  x.item.callback();
}
pending…
Array Traversal
for(var z = 0; z < arry.length; z++) {
    arry[z].callback();
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment