# polymorphic vs monomorphic #9

Closed
opened this Issue Jul 29, 2017 · 2 comments

Projects
None yet
3 participants
Collaborator

### mcollina commented Jul 29, 2017 • edited Edited 1 time mcollina edited Jul 29, 2017 (most recent)

 Jakob Kummerow wrote: I can't reproduce (or explain) your results for "polymorphic functions" with a plain d8 binary. While it always has been and always will be true that polymorphism has a speed cost, it doesn't make sense that a single "obj.length" load amid all the other stuff (creating objects, branching, calling, looping) would cause a 2x slowdown overall, because all the other code (like the empty loop) is not affected by that one polymorphic property load. How can we update the benchmark in a way that is meaningful? cc @bmeurer @fhinkel @hashseed @TimothyGu

### jakobkummerow commented Jul 29, 2017

 ``````function polymorphic() { var objects = [{a:1}, {b:1, a:2}, {c:1, b:2, a:3}, {d:1, c:2, b:3, a:4}]; var sum = 0; for (var i = 0; i < 10000; i++) { var o = objects[i & 3]; sum += o.a; } return sum; } function monomorphic() { var objects = [{a:1}, {a:2}, {a:3}, {a:4}]; var sum = 0; for (var i = 0; i < 10000; i++) { var o = objects[i & 3]; sum += o.a; } return sum; } `````` (only `var objects` is different between the two) The higher the degree of polymorphism, the bigger the performance cost. For two different shapes, I'm seeing about 10-30% more time in the polymorphic compared to the monomorphic version; for four different shapes the delta goes up to 60-100% (it's pretty flaky in repeated runs). Note that all these numbers are very specific to the situation you're testing. If you swap the order of properties in the object -- it makes a difference. If some of the object shapes show up more frequently than others -- it makes a difference. For polymorphism degrees greater than four, even what the rest of your program is doing makes a (possibly huge!) difference. It's extremely difficult to draw simple, generally-true conclusions from this, or give general advice. Polymorphism can be anything from "nothing to worry about" to "the single biggest performance problem with this app".
Owner

### davidmarkclements commented Jul 29, 2017

 Awesome thanks @jakobkummerow I think the generalism we can make is stay away from polymorphism in hot paths because it could trigger a significant performance issue

Merged