test-regexp-cache

JavaScript performance comparison

Test case created by XLee

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var reg = /[\^\[\]\$\(\)\{\}\?\*\.\+]/g;
  function regexpCache(source) {
          return source.replace(reg, function (c) {
              return '\\' + c;
          });
      }
  
  function regexpLiteral(source) {
          return source.replace(/[\^\[\]\$\(\)\{\}\?\*\.\+]/g, function (c) {
              return '\\' + c;
          });
      }
  
  
  var regexpClosure = function (reg) {
      return function (source) {
              return source.replace(reg, function (c) {
                  return '\\' + c;
              });
          }    
  }(/[\^\[\]\$\(\)\{\}\?\*\.\+]/g);

};
</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
new regexp everytime
for (var i =0; i< 10; i++) {
regexpLiteral('^[a-b1-9]+');
}
pending…
regexp cached with variable
for (var i =0; i< 10; i++) {
regexpCache('^[a-b1-9]+');
}
pending…
regexp cached with closure
for (var i =0; i< 10; i++) {
regexpClosure('^[a-b1-9]+');
}
pending…

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

0 Comments