Declarative vs. imperative

Benchmark created by Paul Grenier on


Preparation HTML

<script>
  Array.prototype.every = function (fn) {
      var arr = [], len = this.length, i, t;
      for (i = 0; i < len; i += 1) {
          t = this[i];
          arr.push(fn.call(t, t, i, this));
      }
      return arr;
  };
  Array.prototype.accumulate = function (fn) {
      var len = this.length, i, curr = 0, t;
      for (i = 0; i < len; i += 1) {
          t = this[i];
          curr = fn.call(t, curr, t, i, this);
      }
      return curr;
  };
  function square(n) {
      return n * n;
  }
  function sum(a, b) {
      return a + b;
  }
  var list = [1, 2, 3, 4, 5];
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
d1
list.every(square).accumulate(sum);
ready
i1
var sum = 0,
    n;
for (var i = 0, len = list.length; i < len; i += 1) {
  n = list[i];
  sum += n * n;
}
ready

Revisions

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

  • Revision 1: published by Paul Grenier on