Performance impact of contracts

JavaScript performance comparison

Revision 4 of this test case created by Paul Miller

Info

Performance impact of Contracts.coffee (http://disnetdev.com/contracts.coffee/). You'll need Firefox 5 / 6 / 7+ to test performance, because only firefox currently supports ES5 proxies.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="//disnetdev.com/contracts.coffee/lib/loadContracts.js"></script>
<script src="//disnetdev.com/contracts.coffee/extras/coffee-script.js"></script>
<script>
  var id;
  id = guard(fun([Num], Num, {}),function(x) {
    return x;
  });
 
  var tests = {
    id: function(x) {return x;},
 
    optional: function(n, s, b) {
      return b ? n : s;
    },
 
    list: [42, "foo", [true, 24]],
 
    object: {
      a: "foo",
      b: 42,
      f: function(x) {return x;},
    },
  };
 
  var testsc = {
    id: guard(fun([Num], Num, {}), function(x) {
      return x;
    }).use(),
 
    optional: guard(fun([Num, Str, opt(Bool)], Num, {}), function(n, s, b) {
      return b ? n : s;
    }).use(),
 
    list: guard(arr([Num, Str, arr([Bool, Num])]), [42, "foo", [true, 24]]).use(),
 
    object: guard(object({
      a: Str,
      b: Num,
      f: fun([Num], Num, {})
    }, {}),{
      a: "foo",
      b: 42,
      f: function(x) {
        return x;
      }
    }).use(),
 
  };
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
1 arg, no contracts
tests.id(1)
pending…
1 arg, contracts
testsc.id(1)
pending…
optional, no contracts
tests.optional(1, "str", true)
pending…
optional, contracts
testsc.optional(1, "str", true)
pending…
array, no contracts
tests.list[0] = 55
tests.list[1] = "str"
pending…
array, contracts
testsc.list[0] = 55
testsc.list[1] = "str"
pending…
object, no contracts
tests.object.a = "str"
tests.object.b = 42
 
pending…
object, contracts
testsc.object.a = "str"
testsc.object.b = 42
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:

0 comments

Add a comment