title: Retrieve some meta information from the arbitrary JavaScript object monomorphically (v8)?
body: <p>Question for v8 experts.</p>
  <p>Recently, I've discovered the <a href="https://bugs.chromium.org/p/chromium/issues/detail?id=1137792" rel="nofollow noreferrer">situation with polymorphism in v8</a>. It is that polymorphism is well-optimized only up to the 4 object &quot;shapes&quot;, after which the performance degrades significantly. Classes and object inheritance is ignored. This was a very frustrating discovery, because, with such limitations, well-structured code won't be performant. It seems it is a <a href="https://groups.google.com/g/v8-users/c/qhK1zwUQFeU/m/XU22xRdBBwAJ" rel="nofollow noreferrer">well-known problem since 2017</a> and it is very unlikely anything will change in near future.</p>
  <p>So, I'm willing to implement better polymorphism in user-space: <a href="https://github.com/canonic-epicure/monopoly" rel="nofollow noreferrer">https://github.com/canonic-epicure/monopoly</a></p>
  <p>This is not a novel problem, it is already solved in pretty much any other language out there, with vtables, code specialization etc. Any suggestions how to do it in JavaScript are very welcome.</p>
  <p><strong>The problem I'm currently trying to solve is to retrieve some meta information from the arbitrary object monomorphically.</strong> This meta information will be a vtable analog, containing the information for methods dispatch. This is to avoid an extra box, containing this information.</p>
  <p>The meta information (some object that is shared by many other objects) in JS naturally maps to prototype, so the 1st step is to get the prototype of the object. This can be done monomorphically with <code>Object.getPrototypeOf()</code>. But then, it seems whatever you try, you loose the monomorphicity.</p>
  <p>For example, in the following code, access to the constructor of the object will be megamorphic:</p>
  <pre class="lang-js prettyprint-override"><code>class HasVTable {}
  
  HasVTable.prototype.vtable = {}
  
  class Class1 extends HasVTable {}
  class Class2 extends HasVTable {}
  class Class3 extends HasVTable {}
  class Class4 extends HasVTable {}
  class Class5 extends HasVTable {}
  
  function access(obj) {
      console.log(Object.getPrototypeOf(obj).constructor.name);
  }
  
  %OptimizeFunctionOnNextCall(access);
  
  access(new Class1);
  access(new Class2);
  access(new Class3);
  access(new Class4);
  access(new Class5);
  </code></pre>
  <p><strong>So question is</strong>, how to store some information in the prototype and then retrieve it, w/o loosing monomorphicity? Perhaps, some &quot;well-known&quot; symbols can help here? Or is there some other solution?</p>
  <p>Thank you!</p>
  <hr />
  <p>For example, I've just tried using the iterator symbol with no luck - access to <code>proto</code> in iterator position is still megamorphic:</p>
  <pre class="lang-js prettyprint-override"><code>class HasVTable {}
  
  class Class1 extends HasVTable {
      *[Symbol.iterator] () {
          yield 'Class1'
      }
  }
  class Class2 extends HasVTable {
      *[Symbol.iterator] () {
          yield 'Class2'
      }
  }
  class Class3 extends HasVTable {
      *[Symbol.iterator] () {
          yield 'Class3'
      }
  }
  class Class4 extends HasVTable {
      *[Symbol.iterator] () {
          yield 'Class4'
      }
  }
  class Class5 extends HasVTable {
      *[Symbol.iterator] () {
          yield 'Class5'
      }
  }
  
  function access(obj) {
      const proto = Object.getPrototypeOf(obj)
  
      let res
  
      for (res of proto) break
  
      console.log(res)
  }
  
  %OptimizeFunctionOnNextCall(access);
  
  access(new Class1);
  access(new Class2);
  access(new Class3);
  access(new Class4);
  access(new Class5);
  </code></pre>
  <hr />
  <p>UPDATE 2020/10/21</p>
  <p>I use an excellent <a href="https://www.npmjs.com/package/deoptigate" rel="nofollow noreferrer"><code>deoptigate</code></a> tool to track the code de-optimizations:</p>
  <p><code>npx deoptigate --allow-natives-syntax -r esm src_js/draft3.js</code></p>