Caching Regexes

JavaScript performance comparison

Test case created by Christophe Porteneuve

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var longArray = [];
  for (var index = 0; index < 10000; ++index)
    longArray.push(0 === index % 1000 ? 'test : yeah' : 'foobar');
  

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
No caching
var results = [];
for (var index = 0, len = longArray.length; index < len; ++index) {
  var line = longArray[index];
  if (/[\s\u00a0][:;?!]/.test(line))
    results.push(line);
}
pending…
Caching
var WHITESPACE_AND_DOUBLE_SIGN = /[\s\u00a0][:;?!]/; 
var results = [];
for (var index = 0, len = longArray.length; index < len; ++index) {
  var line = longArray[index];
  if (WHITESPACE_AND_DOUBLE_SIGN.test(line))
    results.push(line);
}
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 Comments