title: Dynamic vs Inline RegExp performance in JavaScript
body: <p>I stumbled upon that performance test, saying that RegExps in JavaScript are not necessarily slow: <a href="http://jsperf.com/regexp-indexof-perf" rel="noreferrer">http://jsperf.com/regexp-indexof-perf</a></p>
  
  <p>There's one thing i didn't get though: two cases involve something that i believed to be exactly the same:</p>
  
  <pre><code>RegExp('(?:^| )foo(?: |$)').test(node.className);
  </code></pre>
  
  <p>And</p>
  
  <pre><code>/(?:^| )foo(?: |$)/.test(node.className);
  </code></pre>
  
  <p>In my mind, those two lines were <strong>exactly</strong> the same, the second one being some kind of shorthand to create a RegExp object. Still, it's <strong>twice faster</strong> than the first.</p>
  
  <p>Those cases are called "dynamic regexp" and "inline regexp".</p>
  
  <p>Could someone help me understand the difference (and the performance gap) between these two?</p>