用于测试固定电话号码模式的最快方法 [英] Fastest method for testing a fixed phone number pattern

查看:401
本文介绍了用于测试固定电话号码模式的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,挑战在于我们正在尝试检测字符串是否与固定电话号码模式匹配,这是一个简单的字符串模式。

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.

模式是:

ddd-ddd-dddd

其中d代表十进制数字而减号代表自身, -

Where "d "represents a decimal digit and the minus symbol represents itself, "-"

目前用于测试的模式是,但如果觉得没有足够的模式来揭穿不正确的格式,可以增加。

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.

"012-345-6789"
"0124-345-6789"
"012-3456-6789"
"012-345-67890"
"01a-345-6789"
"012-34B-6789"
"012-345-678C"
"012"

目标,我寻求的答案是找到执行速度最快的方法返回布尔其中 true 表示模式是好的, false 表示模式不好。

The goal , the answer that I seek, is to find the method that executes the fastest to return a boolean where true means that the pattern is good and false means that the pattern is bad.

这是我目前的解决方案

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

    var i = 0,
        code;

    while (i < 12) {
        code = pattern.charCodeAt(i);

        if (i > 8 || i % 4 !== 3) {
            if (code < 48 || code > 57) {
                return false;
            }
        } else if (code !== 45) {
            return false;
        }

        i += 1;
    }

    return true;
}

可在 jsfiddle 以及测试模式

我有一个 jsperf 创建的地方我将添加进一步的建议方法,以便方法的执行速度可以比较以确定哪个是最快的

I have a jsperf created where I will add further suggested method so that the execution speeds of the methods can be compared to determine which is fastest

您的方法可以是将在浏览器中执行的任何有效的JavaScript代码,如果您这样做,您可以使用ECMA5并定位现代浏览器希望,或使用跨浏览器标准,如果它不在IE6上运行,则答案将被视为不正确。您也可以使用任何您想要的第三方库,即jquery,lodash,下划线等。最后的要求是代码必须不能在Chrome v25或Firefox v20上执行

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

我有什么不清楚的地方请随时发表评论我会更新我的问题以澄清。

I anything is unclear then please feel free to leave a comment and I will update my question to clarify.

仅微优化计数不同的答案

Answers that differ by only micro-optimisations count

如果有效且已添加到性能图表中,请不要更改您的答案。你可以提交1个以上的答案。

Please don't change your answer if it working and has been added to the performance chart. You can submit more than 1 answer.

更新:好的一周过去了,现在是时候宣布我将选择的答案了。

Update: Ok a week has passed and now it is time to announce the answer that I will choose.

从这项练习中学到了什么?

What has been learnt from this exercise?

与手工构建的javascript例程相比,似乎正则表达式相对较慢,尽管对于大多数任务而言足够快。 (至少对于小字符串模式)

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)

没有使用任何第三方库,jquery,非核心等的解决方案。并非如此令人惊讶,但我认为有人可能已经尝试过。

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.

展开的循环似乎仍然是王道。许多人说这些天没有必要,因为浏览器如此先进,但是这个测试仍然表明它们是最好的。

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.

我要感谢所有参与此问题的人,特别是那些实际提交测试代码的人。

I'd like to thank all those that engaged in this question, and especially to those that actually submitted code for testing.

推荐答案

比以前更快:

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) != '-');
}

(简称:每个数字< 8只需要比较一次)

(short: every number < 8 only need compared once)

这篇关于用于测试固定电话号码模式的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆