RegExp complexity

Benchmark created on


Preparation HTML

<script>
var inputs = [ "12", "-12", "+12", "12px", "-12px", "+12px", "12.0", "-12.0", "+12.0", "12.0px", "-12.0px", "+12.0px" ];
var re1 = /^-?\d+(?:px)?$/i;
var re2 = /^[-+]?\d*(?:\d|\.\d+)(?:px)?$/i;
var start = inputs.length - 1, idx, result;
</script>

Setup

for (idx = start; idx >= 0; idx--) {
      inputs[idx] += "";
    }

Test runner

Ready to run.

Testing in
TestOps/sec
/^-?\d+(?:px)?$/i
for (idx = start; idx >= 0; idx--) {
  result = re1.test( "" + inputs[idx] );
}
ready
/^[-+]?\d*(?:\d|\.\d+)(?:px)?$/i
for (idx = start; idx >= 0; idx--) {
  result = re2.test( "" + inputs[idx] );
}
ready

Revisions

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