Note that there are some explanatory texts on larger screens.

plurals
  1. POFastest method for testing a fixed phone number pattern
    text
    copied!<p>So, the challenge is that we are trying to detect if a string matches a fixed phone number pattern, this is a simple string pattern.</p> <p>The pattern is:</p> <pre><code>ddd-ddd-dddd </code></pre> <p>Where "d "represents a decimal digit and the minus symbol represents itself, "-"</p> <p>The patterns that are currently used for testing are, but can be increased if it is felt that there are not enough patterns to debunk an incorrect format.</p> <pre><code>"012-345-6789" "0124-345-6789" "012-3456-6789" "012-345-67890" "01a-345-6789" "012-34B-6789" "012-345-678C" "012" </code></pre> <p><strong>The goal , the answer that I seek, is to find the method that executes the fastest to return a <code>boolean</code> where <code>true</code> means that the pattern is good and <code>false</code> means that the pattern is bad.</strong></p> <p>Here is my current solution</p> <pre><code>function matchesPattern(pattern) { if (pattern.length !== 12) { return false; } var i = 0, code; while (i &lt; 12) { code = pattern.charCodeAt(i); if (i &gt; 8 || i % 4 !== 3) { if (code &lt; 48 || code &gt; 57) { return false; } } else if (code !== 45) { return false; } i += 1; } return true; } </code></pre> <p>It is available on <a href="http://jsfiddle.net/Xotic750/WA88n/" rel="nofollow">jsfiddle</a> along with the test patterns</p> <p>I have a <a href="http://jsperf.com/fastest-method-for-testing-a-fixed-phone-number-pattern/8" rel="nofollow">jsperf</a> created where I will add further suggested method so that the execution speeds of the methods can be compared to determine which is fastest</p> <p>Your method can be any valid javascript code that will execute in the a browser, you can use ECMA5 and target modern browsers if you so wish, or use cross-browser standards, the answer will not be deemed incorrect if it does not run on IE6 for example. You may also use any 3rd party libraries that you wish, i.e. jquery, lodash, underscore, etc etc. The final requirement is that the code must not fail to execute on Chrome v25 or Firefox v20</p> <p>I anything is unclear then please feel free to leave a comment and I will update my question to clarify.</p> <p>Answers that differ by only micro-optimisations count</p> <p>Please don't change your answer if it working and has been added to the performance chart. You can submit more than 1 answer.</p> <p>Update: Ok a week has passed and now it is time to announce the answer that I will choose.</p> <p>What has been learnt from this exercise? </p> <p>It would seem that regexs are comparatively slow, although fast enough for most tasks, when compared to a hand built javascript routine. (at least for small string patterns)</p> <p>There was no solution using any 3rd party library, jquery, undescore etc, nothing. Not so much of a surprise, but I though that someone may have tried.</p> <p>Unrolled loops still appear to be king. Many say that it is not necessary these days as the browsers are so advanced, but this test still showed them to be king of the pile.</p> <p>I'd like to thank all those that engaged in this question, and especially to those that actually submitted code for testing.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload