title: How fast is pattern matching in Scala
body: <p>I'm new to Scala. When I am learning it by reading Scala code written by others, one of the most distinguishing feature I find in Scala code that is different from other languages is its pattern matching. </p>
  
  <p>At the same time I feel the convenience and expressiveness it brings, I can't help being curious of the potential performance cost behind it -- generally speaking, how fast is <code>match</code>?</p>
  
  <p>Firstly, without "advanced" features such as matching parameters of constructors, <code>match</code> in Scala, IMO, is the counterpart of <code>switch-case</code> in other languages. For instance,</p>
  
  <pre><code>color match {
    0 =&gt; println "color is red!"
    1 =&gt; println "color is green!"
    2 =&gt; println "color is blue!"
  }
  </code></pre>
  
  <p>As a novice, I want to know if the code above is exactly as fast as equivalent code in <code>if-else</code> statement?</p>
  
  <p>Secondly, now taking those "advanced" features back, for instance:</p>
  
  <pre><code>expr match {
    Animal(name) =&gt; ...
    Animal(name, age) =&gt; ...
    Animal(_, _, id) =&gt; ...
  }
  </code></pre>
  
  <p>As for the code above or other features of match(list matching, pair matching, etc.), I am curious about how Scala implemented these fancy usage? And most importantly, how fast can I expect these code to be? (Say, are they still as fast as the <code>match</code> in the first case? Or maybe slightly slower? Or extremely slow owing to the use of some technology such as reflection?)</p>
  
  <p>Thanks in advance!</p>