Regex Pattern Test (v2)

Revision 2 of this benchmark created on


Setup

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 splitAndResolveFast(role, template) {
  let roles;
  if (typeof role === 'string') {
    roles = role.split(/\s*,\s*|\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;
}

Test runner

Ready to run.

Testing in
TestOps/sec
Wrapped
splitAndResolveSlow('test,test,test');
ready
Unwrapped
splitAndResolveFast('test,test,test');
ready

Revisions

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