5

I have a function that corrects capitalization for a list of unusually capitalized words:

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);
});

Now the problem I am facing is that most input strings will contain on average between 0 and 3 of these words. Obviously now I am doing dozens (and potentially hundreds; that array has an uncanny tendency to grow over time) of function calls which essentially do nothing.

How can I make this code faster and get rid of the unnecessary function calls?

Example input:

My iphone 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
  • the calls are not uneccessary are they? If you want to check capitalise each string then you'll need to check for each one... Just because it doesn't exist it doesn't mean the check wasn't necessary... Commented Mar 16, 2011 at 11:54
  • @Sam But is necessary over the whole input? Or could a smarter regexp be crafted that would do all the checks in one function call? Commented Mar 16, 2011 at 11:59

1 Answer 1

5

You can build regexp containing all your words, capturing each word by enclosing it in parentheses. Using that in a replace will provide enough information to recover the original word in the replace function.

  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.

This turns out to be faster then the original code.

Sign up to request clarification or add additional context in comments.

4 Comments

The number of function calls this makes is 2 +(number_of_words_replaced). The rest of heavy lifting is done by fast internal functions. You can eliminate the regexp build at each call if the words array is static
The regexp needs to be modified slightly because it then matches within words: RegExp('\\b(?:(' + words.join(')|(') + '))\\b', 'ig') does the trick.
You are correct, I tried but missed by a pair of parentheses ;-)
@HBP Ran those tests in Chrome and Firefox. Firefox was pretty comparable to your Safari run, but Chrome shows your code as being WAY faster. Nice work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.