Quantcast
Channel: upshots » ActionScript 3
Viewing all articles
Browse latest Browse all 10

JS/AS – Cache your RegExp!

$
0
0

In both JavaScript and ActionScript, creating a regular expression – even using literal notation – results in the instantiation of a new RegExp object. If you’re going to use a pattern repeatedly (which is usually the case) – cache your patterns! Just like you would any other object – “literal” doesn’t mean “free”.

Simple test shows performance gains on average between 25% (IE) and 100% (FP9).

var pattern = /^bob$/i;
var good = "bob";
var bad = "ken";

var start = Number(new Date());
for(var i=0;i<10000;i++){
  var g = pattern.test(good);
  var b = pattern.test(bad);
  //var g = /^bob$/i.test(good);
  //var b = /^bob$/i.test(bad);

}
trace(Number(new Date()) - start);

Viewing all articles
Browse latest Browse all 10

Trending Articles