Search code examples
javascriptnode.jsoopv8

V8 won't optimize more than 4 object types?


So I was watching Franziska Hinkelmann's talk on the V8 engine.

She comments at 20:03 that the V8 engine will not optimize if there are more than four object types. I am just starting out in node.js, so I don't have any experience in these matters, but four seems like a very low number. Wouldn't OO software usually involve more than four object types?

I'm aware that V8 has limitations on the number of properties an object can have for fast processing (8, also seems low).


Solution

  • V8 developer here. In the talk you are referencing, the situation Franziska is referring to is when a single place in your code (e.g. a single do_something_with(obj.prop);) sees more than four types (for obj), it switches to a different mode. That's not the same as "doesn't optimize" -- on the contrary, when V8 detects this pattern, it optimizes for it; it just so happens that optimizing for highly polymorphic code requires a different approach than optimizing for monomorphic code.

    And as jfriend00 already commented, you most probably don't have to worry about this internal detail.

    Also, for the record, the number of properties that an object can have in fast-to-access/slow-to-create configuration is about 1,000. (It's another case where there isn't a "good"/"fast" and a "bad"/"slow" state, it's rather "optimize for one" or "optimize for the other".)