reducibles

JavaScript performance comparison

Test case created by Irakli Gozilishvili

Info

Comparing clojure like reducers to native implementations

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    // Some helper functions we'll use
    function increment(x) { return x + 1 }
    function isOdd(n) { return n % 2 }
    function sum(x, y) { return (x || 0) + (y || 0) }
   
    function reduced(value) {
      return Object.create(reduced.prototype, {
        value: { value: value }
      })
    }
    reduced.is = function is(value) {
      return value && value.constructor === reduced
    }
   
    function reducible(reduce) {
      return { reduce: reduce }
    }
   
    function reducer(process) {
      return function(f, items) {
        return reducible(function(next, start) {
          return reduce(function(result, item) {
            var value = process(f, item, reduced)
            var ended = reduced.is(value)
            value = ended ? value.value : value
            result = value === undefined ? result : next(result, value)
            return ended ? reduced(result) : result
          }, items, start)
        })
      }
    }
   
    function reduce(f, reducible, start) {
      return reducible.reduce(f, start)
    }
   
    var filter = reducer(function(f, item) {
      if (f(item)) return item
    })
   
    var map = reducer(function(f, item) {
        return f(item)
    })
   
    function into(reducible, target) {
      // reduce reducible and colelect it's
      // items into array which we return after.
      return reduce(function(result, item) {
        result.push(item)
        return result
      }, reducible, target || [])
    }
   
    function range(from, to) {
      var values = []
      while (from < to) values.push(from++)
      return values
    }
   
};
</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
array
range(1, 100).map(increment).filter(isOdd).reduce(sum);
pending…
reducible
reduce(sum, filter(isOdd, map(increment, range(1, 100))));
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:

1 comment

Add a comment