Testing regex against substring on CSS property analysis

Benchmark created by Martin Doyle on


Description

Compare regex to substring on analysis of CSS dimension

Setup

var testRegex = /([0-9]*[.]?[0-9]*)(%|px|mm|cm|in|pc|pt|px|ex|em|rem|0|none|auto)/i,
      testProperties = ['100%', '1.5rem', 'auto', '26px'],
      propIdx = 0,
      numProps = testProperties.length,
      nextProp = null,
      propInfo = null;

Teardown


    propIdx = 0;
    nextProp = null;
    propInfo = null;
  

Test runner

Ready to run.

Testing in
TestOps/sec
regex
for (propIdx = 0; propIdx < numProps; propIdx++) {
  nextProp = testProperties[propIdx];
  var parts = nextProp.match(testRegex) || [];
  propInfo = {
    amount: parts[1] || '',
    unit: parts[2] || ''
  };
}
ready
substring
for (propIdx = 0; propIdx < numProps; propIdx++) {
  nextProp = testProperties[propIdx];
  var iAt = -1;
  var ch;
  for (var i = 0, imax = nextProp.length; i < imax; i++) {
    ch = nextProp.charCodeAt(i);
    if (!(ch == 45 || ch == 46 || (ch > 47 && ch < 58))) {
      iAt = i;
      break;
    }
  }
  propInfo = {
    amount: iAt > 1 ? nextProp.substring(1, iAt - 1) : null,
    unit: nextProp.substring(iAt)
  };
}
ready

Revisions

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

  • Revision 1: published by Martin Doyle on