title: Performance if (typeof x == 'number')?
body: <p>I've been Googling quite some time for e.g. 'typeof' and 'performance', but I haven't been able to find a satisfactory answer to the following problem.</p>
  
  <p>I am trying to implement complex numbers for the <a href="http://www.transcrypt.org" rel="noreferrer">Transcrypt</a> Python to JavaScript compiler, using local operator overloading. Since we're dealing with a dynamically typed language, it cannot be predicted what type of data will be in a variable.</p>
  
  <p>If I translate <code>x + y</code> to JavaScript, having operator overloading switched on, it will translate e.g. as <code>__add__ (x, y)</code> In order to do the right thing, the <code>__add__</code> function has to check for both <code>x</code> and <code>y</code> whether they are 'ordinary' JavaScript numbers or if one of them or both of them are of type 'complex', since that requires special operations.</p>
  
  <p>The most obvious way to do that is to test for <code>typeof x == 'number'</code>. However, coming from a C/C++ background, it seems ridiculously inefficient to test for equality with a string with six characters which on top of that first has to be retrieved from memory, only to possible add two integers, which for many processors, once parsed, would be only one instruction.</p>
  
  <p>What amazes me most is that checks like this are advised everywhere around the internet as the normal thing to do. Does anyone know if <code>x == 'number'</code> or possible <code>x === 'number'</code> is somehow cleverly optimized to prevent a full string comparison.</p>
  
  <p>To further clarify the problem, here's my current code for the <code>__add__</code> operator, using the string comparison.</p>
  
  <pre><code>def __add__ (self, other):
      if __typeof__ (other) == 'number':   # Translates to: if (typeof other == 'number') {
          return complex (self.real + other, self.imag)
      else:   # Other is complex
          return complex (self.real + other.real, self.imag + other.imag)
  </code></pre>
  
  <p>If not can anyone hint me on a quicker way to distinguish between a number and an arbitrary non-numerical object.</p>
  
  <p><strong>Thanks for the tips.</strong> The source is now:</p>
  
  <pre><code>def __sub__ (self, other):
      if __typeof__ (other, 'number'):
          return complex (self.real - other, self.imag)
      else:
          return complex (self.real - other.real, self.imag - other.imag)
  </code></pre>
  
  <p>translated by:</p>
  
  <pre><code>elif node.func.id == '__typeof__':
      self.emit ('typeof ')
      self.visit (node.args [0])
      self.emit (' === ') # Give JavaScript string interning a chance to avoid char by char comparison
      self.visit (node.args [1])
      return
  </code></pre>
  
  <p>to: </p>
  
  <pre><code>get __add__ () {return __get__ (this, function (self, other) {
      if (typeof other === 'number') {
          return complex (self.real + other, self.imag);
      }
      else {
          return complex (self.real + other.real, self.imag + other.imag);
      }
  });},
  </code></pre>