Regex Pattern Test (v3)

Revision 3 of this benchmark created on


Setup



function splitAndResolveFast(role, template) {
  let roles;
  if (typeof role === 'string') {
    roles = role.split(/[,\s]+/);
  } else if (Array.isArray(role)) {
    roles = role;
  } else {
    return role;
  }

  for (let i = 0; i < roles.length; ++i) {
    roles[i] = roles[i];
  }
  return roles;
}String.customSplit = function(str, delim) {
  const ret = [];
  let foundIdx = str.indexOf(delim);
  if (foundIdx !== -1) {
    let retIdx = 0, lastIdx = 0;
    do {
      ret[retIdx++] = str.substring(lastIdx, foundIdx);
      lastIdx = foundIdx + delim.length;
      foundIdx = str.indexOf(delim, lastIdx);
    } while(foundIdx !== -1);
    if (lastIdx <= str.length) {
      ret[retIdx] = str.substring(lastIdx);
    }
    // ret.length = retIdx;
  } else {
    ret[0] = str;
    // ret.length = 1;
  }

  return ret;
};

String.isString = function(value) {
  // Nothing except a String should have both of these prototypes
  // This is untrue: TemplateInstance can have both functions. The line below has been replaced with a different approach.
  // return !!(value != null && value.substring && value.substr);
  return typeof value === 'string' || value instanceof String;

  // More Proper way to do this would be
  // return Object.prototype.toString.call(object) === "[object String]";
  // But this is significatantly slower on pretty much everything except "Chrome"
};

function splitAndResolveSlow(role, template) {
  //var type = kindOf(role);
  var roles;
  //if(type === 'string') roles = role.split(/(?:\s*,\s*)|(?:\s+)/);
  //else if(type === 'array') roles = role;
  if(String.isString(role)) roles = role.split(/(?:\s*,\s*)|(?:\s+)/);
  else if(Array.isArray(role)) roles = role;
  else return role;

  for(var i = roles.length - 1;i >= 0;i--) {
    roles[i] = roles[i];
  }

  return roles;
}


function splitAndResolveFastest(role, template) {
  let roles;
  if (typeof role === 'string') {
    roles = role.split(/[, ]+/);
  } else if (Array.isArray(role)) {
    roles = role;
  } else {
    return role;
  }

  for (let i = 0; i < roles.length; ++i) {
    roles[i] = roles[i];
  }
  return roles;
}

Test runner

Ready to run.

Testing in
TestOps/sec
Slow
splitAndResolveSlow('test,test,test');
splitAndResolveSlow('andrewwantedalongstringwithnocommas butitisstillverylong andrewwantedalongstringwithnocommasbutitisstillverylong');
ready
Fast?
splitAndResolveFast('test,test,test');
splitAndResolveFast('andrewwantedalongstringwithnocommas butitisstillverylong andrewwantedalongstringwithnocommasbutitisstillverylong');
ready
Fastest?
splitAndResolveFastest('test,test,test');
splitAndResolveFastest('andrewwantedalongstringwithnocommas butitisstillverylong andrewwantedalongstringwithnocommasbutitisstillverylong');
ready

Revisions

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