Monomorphic variable performance

JavaScript performance comparison

Test case created by Kevin Gadd

Info

In SpiderMonkey, if the type of a variable is monomorphic, it performs better

Preparation code

<script type="text/javascript">
  function sumMonomorphic(a, b, c, d) {
    var result = a;
    result += b;
    result += c;
    result += d;
    return result;
  }

  function sumDimorphic(a, b, c, d) {
    var result;
    result = a;
    result += b;
    result += c;
    result += d;
    return result;
  }

  function sumPolymorphic(a, b, c, d) {
    var result;
    result = null;
    result = false;
    result = "";

    result = a;
    result += b;
    result += c;
    result += d;
    return result;
  }

  var count = 5000;
</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
Monomorphic local variable
for (var i = 0, fn = sumMonomorphic; i < count; i++)
  if (fn(1, 2, 3, 4) !== (1 + 2 + 3 + 4))
    throw new Error();
pending…
Dimorphic local variable
for (var i = 0, fn = sumDimorphic; i < count; i++)
  if (fn(1, 2, 3, 4) !== (1 + 2 + 3 + 4))
    throw new Error();
pending…
Polymorphic local variable
for (var i = 0, fn = sumPolymorphic; i < count; i++)
  if (fn(1, 2, 3, 4) !== (1 + 2 + 3 + 4))
    throw new Error();
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