Immutable.js vs lazy iterators (v3)

Revision 3 of this benchmark created by bergi on


Preparation HTML

<script src="https://rawgit.com/facebook/immutable-js/master/dist/immutable.min.js"></script>
<script>
var iterator = (function () {
    var min = Math.min;

    Iterator.prototype = {
        map: function (f) {
            return new Iterator(compose(f, this.at), this.length);
        },
        take: function (n) {
            return new Iterator(this.at, min(n, this.length));
        },
        toArray: function () {
            var at     = this.at;
            var length = this.length;
            var array  = new Array(length);
            var index  = 0;

            while (index < length) array[index] = at(index++);

            return array;
        }
    };

    return iterator;

    function compose(f, g) {
        return function (x) {
            return f(g(x));
        };
    }

    function iterator(a) {
        return new Iterator(function (index) {
            return a[index];
        }, a.length);
    }

    function Iterator(at, length) {
        this.at     = at;
        this.length = length;
    }
}());

var fusion = (function () {
    var min = Math.min;
 
    Iterator.prototype = {
        map: function (f) {
            return new MapIterator([f, this.at], this.length);
        },
        take: take,
        toArray: toArray
    };

    MapIterator.prototype = {
        map: function (f) {
            return new MapIterator([f].concat(this.fs), this.length);
        },
        take: take,
        toArray: toArray
    };

    return iterator;
 
    function compose(f, g) {
        return function (x) {
            return f(g(x));
        };
    }
 
    function iterator(a) {
        return new Iterator(function (index) {
            return a[index];
        }, a.length);
    }
 
    function Iterator(at, length) {
        this.at     = at;
        this.length = length;
    }

    function MapIterator(fs, length) {
        this.fs     = fs;
        this.at     = fs.reduce(compose);
        this.length = length;
    }

    function take(n) {
        return new Iterator(this.at, min(n, this.length));
    }

    function toArray() {
        var at     = this.at;
        var length = this.length;
        var array  = new Array(length);
        var index  = 0;
 
        while (index < length) array[index] = at(index++);
 
        return array;
    }
}());
</script>

Setup

function square(x) {
        return x * x;
    }
    function increment(x) {
        return x + 1;
    }
    var arr = new Array(10000);
    for (var i=0; i<arr.length; i++)
        arr[i] = Math.random();
    var immu = Immutable.Seq(arr).map(square).map(increment).take(8000),
        lazy = iterator(arr).map(square).map(increment).take(8000),
        fuse = fusion(arr).map(square).map(increment).take(8000)
    
    var l=0;

Teardown


    l=0;
  

Test runner

Ready to run.

Testing in
TestOps/sec
Immutable.js iteration
l += immu.toArray().length;
ready
Lazy iteration
l += lazy.toArray().length;
ready
Lazy short-cut fusion iteration
l += fuse.toArray().length;
ready

Revisions

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

  • Revision 1: published by Aadit M Shah on
  • Revision 2: published by bergi on
  • Revision 3: published by bergi on