looper-3924893824

Benchmark created on


Setup

function nop() {}
    
    function Chain() {
        this._tail = {};
        this._head = {next: this._tail};
    } Chain.prototype = {
        append: function (data) {
            this._tail.data = data;
            this._tail = this._tail.next = {};
        },
        each: function (fn) {
            var node = this._head,
                prevNode = node,
                tail = this._tail;
    
            function stamp() {
                prevNode.next = node.next;
            }
    
            while ((node = node.next) !== tail) {
                fn(node.data, stamp);
                prevNode = node;
            }
        },
        isEmpty: function () {
            return this._head.next === this._tail;
        }
    };
    
    function Chain_stripped() {
        this._tail = {};
        this._head = {next: this._tail};
    } Chain_stripped.prototype = {
        append: function (data) {
            this._tail.data = data;
            this._tail = this._tail.next = {};
        },
        each: function (fn) {
            var node = this._head,
                prevNode = node,
                tail = this._tail;
    
            while ((node = node.next) !== tail) {
                fn(node.data);
                prevNode = node;
            }
        },
        isEmpty: function () {
            return this._head.next === this._tail;
        }
    };
    
    function Chain_manual(msg) {
        this._tail = {};
        this._head = {next: this._tail};
        this._node = this._head;
        this._prevNode = this._head;
    } Chain_manual.prototype = {
        append: function (data) {
            this._tail.data = data;
            this._tail = this._tail.next = {};
        },
        rewind: function () {
            this._node = this._head;
        },
        next: function () {
            this._prevNode = this._node;
            this._node = this._node.next;
            return this._node.data;
        },
        stamp: function () {
            this._prevNode.next = this._node.next;
        },
        isEmpty: function () {
            return this._node.next === this._tail;
        }
    };

Test runner

Ready to run.

Testing in
TestOps/sec
array
var i, j, a = [];
for (i = 0; i < 100; i++) {
    a.push(i);
}
for (i = 0; i < 100; i++) {
    for (j = 0; j < a.length; j++) {
        t = a[j];
        nop(t);
    }
}
ready
Chain
var i, a = new Chain();
for (i = 0; i < 100; i++) {
    a.append(i);
}
for (i = 0; i < 100; i++) {
    a.each(function (v) {
        nop(v);
    });
}
ready
Chain stripped
var i, a = new Chain_stripped();
for (i = 0; i < 100; i++) {
    a.append(i);
}
for (i = 0; i < 100; i++) {
    a.each(function (v) {
        nop(v);
    });
}
ready
Chain manual
var i, v, a = new Chain_manual();
for (i = 1; i < 101; i++) {
    a.append(i);
}
for (i = 0; i < 100; i++) {
    while (v = a.next()) {
        nop(v);
    }
    a.rewind();
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.