JavaScript正则表达式性能

5

我有一个函数,可以为一组不规则大写单词进行纠正大小写的操作:

var line = "some long string of text";
["AppleScript", "Bluetooth", "DivX", "FireWire", "GarageBand", 
 "iPhone", "iTunes", "iWeb", "iWork", "JavaScript", "jQuery", "MacBook", 
 "MySQL", "PowerBook", "PowerPoint", "QuickTime", "TextEdit", "TextMate",
 // ... 
 "Wi-Fi", "Xcode", "Xserve", "XMLHttpRequest"].forEach(function(name) {
      line = line.replace(RegExp(name, "gi"), name);
});

现在我面临的问题是,大多数输入字符串平均包含0到3个这些单词。显然,我正在进行几十次(甚至可能是数百次;该数组有一种神奇的倾向随着时间的推移而增长)基本上什么也不做的函数调用。

如何使此代码更快并摆脱不必要的函数调用?

示例输入:

我的iPhone应用程序在UIViewController下有一个用户表单。当我再次启动应用程序时,我的某些UIView会改变其位置和大小。(这些UIView依赖于键盘位置)某处肯定是我的错。我尝试弄清楚当应用程序从后台重新启动时发生了什么以及可以进行UIView更改的位置。


这些调用不是不必要的,对吧?如果你想检查每个字符串的大写形式,那么你需要为每个字符串进行检查...仅仅因为它不存在并不意味着检查是不必要的... - Sam Holder
@Sam 但是需要在整个输入中进行吗?或者能否创建一个更聪明的正则表达式,在一次函数调用中完成所有检查? - Jakub Hampl
1个回答

5

你可以建立包含所有单词的正则表达式,通过将每个单词括在括号中进行捕获。在替换中使用它将提供足够的信息来恢复替换函数中的原始单词。

  function correct (text, words) {
    return text.replace (RegExp ('\\b(?:(' + words.join (')|(') + '))\\b', 'ig'), function (m) {
      for (var a = arguments.length - 2; a--;)
        if (arguments[a])
      return words[a-1] || m;
    });
  } 

  console.log (correct ("My iphone itunes divx firewire application has a user form under uiviewcontroller. When I start application again some of my uiview changes its positions and sizes. (These uiviews depend on keyboard position) Somewhere is definitely my fault. I try to figure what is going on when application starts again from background and where the uiview changes can be done.",
    ["AppleScript", "Bluetooth", "DivX", "FireWire", "GarageBand", 
 "iPhone", "iTunes", "iWeb", "iWork", "JavaScript", "jQuery", "MacBook", 
 "MySQL", "PowerBook", "PowerPoint", "QuickTime", "TextEdit", "TextMate",
 // ... 
 "UIViewController","UIView",
 "Wi-Fi", "Xcode", "Xserve", "XMLHttpRequest"]));
My iPhone iTunes DivX FireWire application has a user form under UIViewController. When I start application again some of my UIView changes its positions and sizes. (These UIViews depend on keyboard position) Somewhere is definitely my fault. I try to figure what is going on when application starts again from background and where the UIView changes can be done.

这比原始代码更快


这将产生2 +(number_of_words_replaced)次函数调用。其余的重活都由快速内部函数完成。如果单词数组是静态的,您可以在每次调用时消除正则表达式构建。 - HBP
1
正则表达式需要稍微修改一下,因为它会匹配单词内的内容:RegExp('\\b(?:(' + words.join(')|(') + '))\\b', 'ig') 可以解决这个问题。 - Jakub Hampl
你说得对,我尝试了但是错了一对括号;-) - HBP
@HBP 在Chrome和Firefox中运行了这些测试。Firefox与您的Safari运行相当,但Chrome显示您的代码速度要快得多。干得好! - joequincy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接