Skip to content
David Nolen edited this page Jan 16, 2015 · 18 revisions

Mori now provides an api suitable for benchmarking its persistent data structure from plain JavaScript. Operations like mori.assoc are variadic and must internally dispatch to the correct arity handler through the length of arguments, the overheads incurred are very measurable. To eliminate this overhead Mori now support directs invocation of function arities by exporting a simple direct api.

var m = require("mori"),
    h = m.hashMap();

m.assoc(h, "foo", "bar"); // indirect
m.assoc.f3(h, "foo", "bar"); // direct

The convention is simply fN where N is some integer representing the arity. Note some function only have a single defined arity and a fN direct invocation will not be available.

Here is a complete example with Mori and Immutable.js

var m = require("mori"),
    Immutable = require("immutable");

function time(f, iters) {
    iters = iters || 1;
    for(var i = 0; i < iters; i++) {
        var s = new Date();
        f();
        console.log("Elapsed "+((new Date()).valueOf()-s.valueOf())+"ms");
        console.log("----------");
    }
}

// ~180ms Node 0.10.35, 2.26ghz 2010 MBP
time(function() {
    var hm = m.hashMap();
    for(var i = 0 ; i < 100000; i++) {
        for(var j = 0; j < 8; j++) {
            hm = m.assoc.f3(hm, "foo"+j, j);
        }
    }
}, 1);

// ~2000ms
time(function() {
    var hm = Immutable.Map();
    for(var i = 0 ; i < 100000; i++) {
        for(var j = 0; j < 8; j++) {
            hm = hm.set("foo"+j, j);
        }
    }
}, 1);

Clone this wiki locally