Fastest way to check if a JS variable starts with a number
<p>I am using an object as a hash table and I have stuffed both regular properties and integers as keys into it. </p>

<p>I am now interested in counting the number of keys in this object which are numbers, though obviously a <code>for (x in obj) { if (typeof x === "number") { ... } }</code> will not produce the result I want because all keys are strings. </p>

<p>Therefore I determined that it is sufficient for my purposes to assume that if a key's first character is a number then it must be a number so I am not concerned if key "3a" is "wrongly" determined to be a number. </p>

<p>Given this relaxation I think i can just check it like this </p>

<pre><code>for (x in obj) {
  var charCode = x.charCodeAt(0);
  if (charCode &lt; 58 &amp;&amp; charCode &gt; 47) { // ascii digits check
     ...
  }
}
</code></pre>

<p>thereby avoiding a regex and <code>parseInt</code> and such. </p>

<p>Will this work? <code>charCodeAt</code> is JS 1.2 so this should be bullet-proof, yes?</p>

<p>Hint: I would love to see a jsperf comparing my function with what everyone comes up with. :) I'd do it myself but jsperf confuses me </p>

<p>Update: Thanks for starting up the JSPerf, it confirms my hope that the <code>charCodeAt</code> function would be executing a very quick piece of code reading out the int value of a character. The other approaches involve parsing. </p>
