string.replaceAll

JavaScript performance comparison

Test case created by Simon

Info

Compare various string.replaceAll() methods for case-insensitive replacement. RegEx vs split/indexOf/join.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var replaceAll_1 = (function() {
      //replaceAll using split/indexOf/join
      function replaceAll(string, search, replace, ignoreCase) {
        if (ignoreCase) {
          var result = [];
          var _string = string.toLowerCase();
          var _search = search.toLowerCase();
          var start = 0, match, length = _search.length;
          while ((match = _string.indexOf(_search, start)) >= 0) {
            result.push(string.slice(start, match));
            start = match + length;
          }
          result.push(string.slice(start));
        } else {
          result = string.split(search);
        }
        return result.join(replace);
      }
   
      return replaceAll;
    })();
   
    var replaceAll_2 = (function() {
      //replaceAll using RegEx (escaping search string and replace string)
      function replaceAll(string, search, replace, ignoreCase) {
        var regex = new RegExp(escapeRegEx(search), ignoreCase ? 'ig' : 'g');
        replace = replace.replace(/\$/g, '$$');
        return string.replace(regex, replace);
      }
   
      function escapeRegEx(str) {
        return str.replace(/[.?*+^$\[\]\\(){}-]/g, '\\$&');
      }
   
      return replaceAll;
    })();
   
    var shortString = 'Bananas And Bran';
   
    var longString = 'Cras placerat, lectus eu mollis ullamcorper, tortor lorem malesuada orci, quis pellentesque velit orci sed metus. Aliquam eget augue est, id porttitor erat. Nullam placerat nulla in velit vestibulum quis euismod justo vulputate. Integer ullamcorper, ligula at sodales ullamcorper, sem arcu pharetra purus, vel tempus nisl quam sit amet ipsum. Integer mattis tempor nibh eu mollis. Maecenas quis dolor leo, et pretium nibh. Vestibulum sed dapibus enim. Cras odio leo, eleifend dictum commodo id, malesuada sed nunc. Duis ornare commodo odio ac convallis. Suspendisse convallis sagittis erat, quis iaculis lectus sodales ut. Praesent sit amet quam et diam sagittis ornare. Integer eleifend condimentum interdum. Morbi ut ipsum quis arcu tempus viverra. Fusce suscipit odio sit amet lacus consectetur quis tempor nunc eleifend. Duis porta ipsum non quam porttitor ultrices.';
   
    var longerString = longString.replace(/\Sa/g, 'Duis eget mi id sapien fringilla ullamcorper.');
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
replaceAll using split/indexOf/join
replaceAll_1(shortString, 'AN', '-', true);
replaceAll_1(shortString, 'AN', 'Maecenas quis dolor leo, et pretium nibh.', true);

//short search values, short and long replace values
replaceAll_1(longString, 'a', '-', true);
replaceAll_1(longString, 'a', 'Aliquam vitae ante neque. Cras ultrices ornare congue. Nam sit amet congue risus. Nam consequat quam id est varius a ultrices erat mollis. Vivamus a felis et velit ullamcorper ullamcorper sed in arcu. Phasellus luctus eros eget sem vehicula tincidunt. Sed nisi velit, lacinia eu iaculis auctor, pharetra id lacus.', true);

//longer search values (short and long replace values)
replaceAll_1(longerString, 'Duis eget mi id sapien fringilla ullamcorper.', '-', true);
replaceAll_1(longerString, 'Duis eget mi id sapien fringilla ullamcorper.', 'Nunc porta mattis erat nec porta. Nam eget tempor odio. Nunc egestas, sem sollicitudin laoreet fermentum, dui nibh tristique turpis, quis aliquam tellus risus nec sem. Suspendisse potenti. Aenean vitae leo erat, in malesuada urna. Nullam vestibulum lectus sed odio tincidunt a mollis dolor egestas. Nullam eget laoreet neque.', true);
pending…
replaceAll using RegEx
replaceAll_2(shortString, 'AN', '-', true);
replaceAll_2(shortString, 'AN', 'Maecenas quis dolor leo, et pretium nibh.', true);

//short search values, short and long replace values
replaceAll_2(longString, 'a', '-', true);
replaceAll_2(longString, 'a', 'Aliquam vitae ante neque. Cras ultrices ornare congue. Nam sit amet congue risus. Nam consequat quam id est varius a ultrices erat mollis. Vivamus a felis et velit ullamcorper ullamcorper sed in arcu. Phasellus luctus eros eget sem vehicula tincidunt. Sed nisi velit, lacinia eu iaculis auctor, pharetra id lacus.', true);

//longer search values (short and long replace values)
replaceAll_2(longerString, 'Duis eget mi id sapien fringilla ullamcorper.', '-', true);
replaceAll_2(longerString, 'Duis eget mi id sapien fringilla ullamcorper.', 'Nunc porta mattis erat nec porta. Nam eget tempor odio. Nunc egestas, sem sollicitudin laoreet fermentum, dui nibh tristique turpis, quis aliquam tellus risus nec sem. Suspendisse potenti. Aenean vitae leo erat, in malesuada urna. Nullam vestibulum lectus sed odio tincidunt a mollis dolor egestas. Nullam eget laoreet neque.', true);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment