Monomorphic vs Polymorphic functions

Benchmark created by Steve Kane on


Description

In V8 in particular, many functions may be valid for various input types. When you pass in different "types" into a function, you are causing that function to be polymorphic which is more difficult to optimize. Here we test the classic "add" function which uses the overloaded "+" operator in javascript to either sum numbers of concatenate strings.

Setup

var addMono = function (a,b) { return a + b }
    var addPoly = function (a,b) { return a + b }
    
    /* 
    here we "prime" V8 by actually using our functions.  This flips internal switches inside the inline-class information for these functions.
    */
    var dummy1 = addMono(1,2)
    var dummy2 = addPoly(1,2)
    var dummy3 = addPoly("mr", "bean")

Test runner

Ready to run.

Testing in
TestOps/sec
monomorphic
for (var i = 0; i < 20000; ++i) {
  addMono(1,2)
}
ready
polymorphic
for (var i = 0; i < 20000; ++i) {
  addPoly(1,2)
}
ready

Revisions

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

  • Revision 1: published by Steve Kane on