Run details:
Mozilla/5.0 (X11; Linux x86_64; rv:89.0) Gecko/20100101 Firefox/89.0
Firefox 89
Linux
Desktop
3 months ago
Test name Executions per second
Memoize typeof call 451.8 Ops/sec
Call typeof every time 480.6 Ops/sec
Tests:
  • Memoize typeof call

    x
     
    let cachedGlobal;
    function getGlobal() {
      if (cachedGlobal) {
        return cachedGlobal;
      }
      
      if (typeof globalThis !== 'undefined' && globalThis) {
        cachedGlobal = globalThis;
      }
      
      return cachedGlobal
    }
    for (let i = 0; i < 4000; i++) {
      const global = getGlobal();
    }
  • Call typeof every time

     
    function getGlobal() {
      if (typeof globalThis !== 'undefined' && globalThis) {
        return globalThis;
      }
    }
    for (let i = 0; i < 4000; i++) {
      const global = getGlobal();
    }