singular and plural function comparison

JavaScript performance comparison

Test case created by keville

Info

compares approaches of handling sets of inputs vs. a single input

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var widget = {
   
      typeAForOne: function (item) {
        this.doSomethingWithItem(item);
      },
   
      typeAForMany: function (items) {
        for (var i = 0, count = items.length; i < count; i++) {
          this.typeAForOne(items[i]);
        }
      },
   
      typeBForOne: function (item) {
        this.typeBForMany([item]);
      },
   
      typeBForMany: function (items) {
        for (var i = 0, count = items.length; i < count; i++) {
          this.doSomethingWithItem(items[i]);
        }
      },
   
      doSomethingWithItem: function (item) {
        item && item.value;
      }
    };
    var items = [];
    var itemCount = 100;
    while (itemCount--) {
      items.push({value:itemCount});
    }
};
</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
type A
widget.typeAForOne({value:1});
widget.typeAForMany(items);
pending…
type B
widget.typeBForOne({value:1});
widget.typeBForMany(items);
pending…
foreach A
widget.doSomethingWithItem({value:1});
items.forEach(function (item) {
  widget.doSomethingWithItem(item);
});
pending…
foreach B
widget.doSomethingWithItem({value:1});
items.forEach(widget.doSomethingWithItem, widget);
pending…

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

Compare results of other browsers

0 comments

Add a comment