regex templating

Benchmark created by regex templating 2 on


Preparation HTML

<script>
  var data1 = {
      "test1": "value1",
      "test2": "value2",
      "test3": "value3"
    }
    
    var template1 = "This is {test1} to see how fast different {test2}-replacement techniques perform! ({test3})";
    
    var sRE = /{([^}]+)}/g;
    var sRE2 = /{(\w+)}/g;
    var sREi = /{([^}]+)}/ig;
    var sREi2 = /{(\w+)}/ig;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Dynamic Regular Expressions
var template = template1;
for (var i in data1) {
  template = template.replace(new RegExp('{' + i + '}', 'g'), data1[i]);
}
ready
Static Regular Expression
var o = "",
    m = null,
    preIndex = 0;
while (m = sRE2.exec(template1)) {
  o += RegExp.leftContext.substr(preIndex) + data1[m[1]];
  preIndex = sRE2.lastIndex;
}
o += RegExp.rightContext;
ready
My Static Regular Expression
var o = [],
    m = null,
    preIndex = 0,
    result = "";
while (m = sRE2.exec(template1)) {
  o.push(RegExp.leftContext.substr(preIndex));
  o.push(data1[m[1]]);
  preIndex = sRE2.lastIndex;
}
o.push(RegExp.rightContext);
result = o.join("");
ready

Revisions

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

  • Revision 1: published by regex templating 2 on