reducibles

JavaScript performance comparison

Revision 3 of this test case created by Irakli Gozilishvili

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 = false //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 CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
array
range(1, 1000000).map(increment).filter(isOdd).reduce(sum);
pending…
reducible
reduce(sum, filter(isOdd, map(increment, range(1, 1000000))));
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.

0 Comments