title: Javascript optimization, V8 engine and Typescript
body: <p>I read the following article about V8 engine and javascript optimization.</p>
  <p><a href="https://blog.sessionstack.com/how-javascript-works-inside-the-v8-engine-5-tips-on-how-to-write-optimized-code-ac089e62b12e" rel="nofollow noreferrer">How JavaScript works: inside the V8 engine + 5 tips on how to write optimized code</a></p>
  <p>According to above article, these are recommended :</p>
  <p>a. V8 engine uses hidden classes and caching mechanism for the hidden classes and properties. So avoid dynamically adding object properties, and avoid dynamically changing the type of properties. Instead use object constructor function.</p>
  <p>b. V8 engine applies more optimizations to frequently used functions. So avoid generating many different similar form functions executed only once. Instead write code to repeatedly execute same function.</p>
  <p>Then, I have 3 questions :</p>
  <p>i. Can 'a' and 'b' be applied to other browser's javascript engine, such as firefox's SpiderMonkey?</p>
  <p>ii. According to 'a', using typescript is more advantageous for v8 engine optimization? (Using typescript, you can generate javascript code with fewer dynamically changing objects and properties)</p>
  <p>iii. Modern JavaScript code uses a lot of inline and anonymous functions having similar form, like the example code below. According to 'b', using single predefined function is more advantageous instead? (This example code is very simplified one to show similar form functions used many times)</p>
  <pre><code>prm.then((res) =&gt; {
      console.log(res);
  }).then((res2) =&gt; {
      console.log(res2);
  }).catch((err) =&gt; {
      console.log(err);
  });
  </code></pre>