Linked List vs Array Traversal

JavaScript performance comparison

Revision 3 of this test case created by Jim Cowart

Preparation code

 
<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 list = new LinkedList();
    var arry = [];
    var i = 10000
    while(i > 0) {
      var _i = i--;
      list.add(!!_i % 3 ? _i : "BOO!");
      arry.push(!!_i % 3 ? _i : "BOO!");
    }
};
</script>

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) {
  // nuthin'
}
pending…
Array Traversal
for(var z = 0; z < arry.length; z++) {
    // nuthin'
}
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