Fastest method for testing a fixed phone number pattern

一世执手 提交于 2019-11-30 21:19:51

even faster than 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 only need compared once)

function whyNotBeSilly(pattern) {
  return !(pattern.length !== 12 ||
           (code = pattern.charCodeAt(0)) < 48 || code > 57 ||
           (code = pattern.charCodeAt(1)) < 48 || code > 57 ||
           (code = pattern.charCodeAt(2)) < 48 || code > 57 ||
           (code = pattern.charCodeAt(4)) < 48 || code > 57 ||
           (code = pattern.charCodeAt(5)) < 48 || code > 57 ||
           (code = pattern.charCodeAt(6)) < 48 || code > 57 ||
           (code = pattern.charCodeAt(8)) < 48 || code > 57 ||
           (code = pattern.charCodeAt(9)) < 48 || code > 57 ||
           (code = pattern.charCodeAt(10)) < 48 || code > 57 ||
           (code = pattern.charCodeAt(11)) < 48 || code > 57 ||
           pattern.charAt(3) != '-' || pattern.charAt(7) != '-');
}

you can use regex for that:

if(/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/.test('123-456-7890'))
    //ok
else
    //not ok

A slight variation of one of the other answers, but I believe the built-in character class should be slightly faster than a custom one:

return /^\d{3}-\d{3}-\d{4}$/.test(phoneNumber);

Declaring a regex object pre-compiles it for any future use. Since you are looping through several test strings, it should perform better to instantiate the object outside the function first:

var rex = /^\d{3}-\d{3}-\d{4}$/;

Then the function would be:

function matchesPattern(pattern) {
    return rex.test(pattern);
}

very fast:

   function tecjam3(pattern) {
  if (pattern.length !== 12) {
    return false;
  }

  code = pattern.charCodeAt(0);
  if (code < 48 || code > 57) return false;
  code = pattern.charCodeAt(1);
  if (code < 48 || code > 57) return false;
  code = pattern.charCodeAt(2);
  if (code < 48 || code > 57) return false;

  code = pattern.charCodeAt(4);
  if (code < 48 || code > 57) return false;
  code = pattern.charCodeAt(5);
  if (code < 48 || code > 57) return false;
  code = pattern.charCodeAt(6);
  if (code < 48 || code > 57) return false;

  code = pattern.charCodeAt(8);
  if (code < 48 || code > 57) return false;
  code = pattern.charCodeAt(9);
  if (code < 48 || code > 57) return false;
  code = pattern.charCodeAt(10);
  if (code < 48 || code > 57) return false;
  code = pattern.charCodeAt(11);
  if (code < 48 || code > 57) return false;

  if (pattern.charAt(3) != '-' || pattern.charAt(7) != '-') return false;

  return true;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!