Multiple Regexp Replace versus Replace Loop

Benchmark created by Jeffrey on


Description

Is it faster to loop through an array of strings and call replace on each one? Or is it faster to call a large regexp with all of the strings at once.

Setup

function stripBusinessName1(business_name) {
      //TODO: find original parser, I had a very good function for this
      business_name = business_name.toLowerCase();
      return business_name.replace(/corporation|\sand\s|\sllc|\sdba|\sbros|\sbrothers|\ssons|\scompany|\sincorporated/g, '').replace(/co$|inc$|corp$/g, '');
    }
    
    function stripBusinessName2(business_name) {
      business_name = business_name.toLowerCase();
      var remove_these = ['corporation', ' and ', ' llc', ' dba', ' bros', ' brothers', ' sons', ' company', ' incorporated'];
      //co and inc can't be reliably removed through traditionally means, they must be at the end
      var len = remove_these.length;
      for (var i = 0; i < len; i++) {
        business_name = business_name.replace(remove_these[i], '');
      }
      return business_name.replace(/co$|inc$|corp$/g, '');
    }
    
    function stripBusinessName3(business_name) {
        //TODO: find original parser, I had a very good function for this
        business_name = business_name.toLowerCase();
     return business_name.replace(/corporation|\sand\s|\sllc|\sdba|\sbros|\sbrothers|\ssons|\scompany|\sincorporated|co$|inc$|corp$/g, '');
    }

Test runner

Ready to run.

Testing in
TestOps/sec
RegExp
stripBusinessName1('Mario and Luigi Brothers aka Super Mario Bros and Nintendo and Sons Incorporated Company Corporation Inc');
ready
Loop
stripBusinessName2('Mario and Luigi Brothers aka Super Mario Bros and Nintendo and Sons Incorporated Company Corporation Inc');
ready
RegExp Unchained
stripBusinessName3('Mario and Luigi Brothers aka Super Mario Bros and Nintendo and Sons Incorporated Company Corporation Inc');
ready

Revisions

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