javascript - Fastest method for testing a fixed phone number pattern -


so, challenge trying detect if string matches fixed phone number pattern, simple string pattern.

the pattern is:

ddd-ddd-dddd 

where "d "represents decimal digit , minus symbol represents itself, "-"

the patterns used testing are, can increased if felt there not enough patterns debunk incorrect format.

"012-345-6789" "0124-345-6789" "012-3456-6789" "012-345-67890" "01a-345-6789" "012-34b-6789" "012-345-678c" "012" 

the goal , answer seek, find method executes fastest return boolean true means pattern , false means pattern bad.

here current solution

function matchespattern(pattern) {     if (pattern.length !== 12) {         return false;     }      var = 0,         code;      while (i < 12) {         code = pattern.charcodeat(i);          if (i > 8 || % 4 !== 3) {             if (code < 48 || code > 57) {                 return false;             }         } else if (code !== 45) {             return false;         }          += 1;     }      return true; } 

it available on jsfiddle along test patterns

i have jsperf created add further suggested method execution speeds of methods can compared determine fastest

your method can valid javascript code execute in browser, can use ecma5 , target modern browsers if wish, or use cross-browser standards, answer not deemed incorrect if not run on ie6 example. may use 3rd party libraries wish, i.e. jquery, lodash, underscore, etc etc. final requirement code must not fail execute on chrome v25 or firefox v20

i unclear please feel free leave comment , update question clarify.

answers differ micro-optimisations count

please don't change answer if working , has been added performance chart. can submit more 1 answer.

update: ok week has passed , time announce answer choose.

what has been learnt exercise?

it seem regexs comparatively slow, although fast enough tasks, when compared hand built javascript routine. (at least small string patterns)

there no solution using 3rd party library, jquery, undescore etc, nothing. not of surprise, though may have tried.

unrolled loops still appear king. many not necessary these days browsers advanced, test still showed them king of pile.

i'd thank engaged in question, , submitted code testing.

even faster before:

function tecjam5(pattern) {     var c;     return !(pattern.length != 12 ||     !(((c=pattern.charcodeat(2))>>3) == 6 || (c>>1) == 28) ||     !(((c=pattern.charcodeat(4))>>3) == 6 || (c>>1) == 28) ||     !(((c=pattern.charcodeat(11))>>1) == 28 || (c>>3) == 6) ||     !(((c=pattern.charcodeat(0))>>3) == 6 || (c>>1) == 28) ||     !(((c=pattern.charcodeat(1))>>3) == 6 || (c>>1) == 28) ||     !(((c=pattern.charcodeat(5))>>3) == 6 || (c>>1) == 28) ||     !(((c=pattern.charcodeat(6))>>3) == 6 || (c>>1) == 28) ||     !(((c=pattern.charcodeat(8))>>3) == 6 || (c>>1) == 28) ||     !(((c=pattern.charcodeat(9))>>3) == 6 || (c>>1) == 28) ||     !(((c=pattern.charcodeat(10))>>1) == 28 || (c>>3) == 6) ||     pattern.charat(3) != '-' || pattern.charat(7) != '-'); } 

(short: every number < 8 need compared once)


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -