Regex Matching

JavaScript performance comparison

Test case created

Info

See http://stackoverflow.com/questions/5324757/javascript-regexp-performance

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var string = "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.";
   
    var words = ["AMD", "AppleScript", "ASUS", "ATI", "Bluetooth", "DivX", "DVD", "Eee PC", "FireWire",
             "GarageBand", "GHz", "iBookstore", "iCal", "iChat", "iLife", "iMac", "iMovie", "iOS", "iPad",
             "iPhone", "iPhoto", "iPod", "iTunes", "iWeb", "iWork", "JavaScript", "jQuery", "Lenovo",
             "MacBook", "MacPorts", "MHz", "MobileMe", "MySQL", "Nvidia", "OS X", "PowerBook", "PowerPoint",
             "QuickTime", "SSD", "TextEdit", "TextMate", "ThinkPad", "USB", "VMware", "WebKit", "Wi-Fi",
             "Windows XP", "WordPress", "Xcode", "XMLHttpRequest", "Xserve"];
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Regexp for each
var line = string;

words.forEach(function(name) {
  line = line.replace(RegExp(name, "gi"), name);
});
pending…
Compiled regexp
var line = string;

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

for(var i = 0, l = words.length; i < l; i++) {
  line = line.replace(RegExp(words[i], "gi"), words[i]);
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment