string compare perf test (v6)

Revision 6 of this benchmark created on


Description

Compare searching for a substring using indexOf and search. This assumes case-insensitive searching, so indexOf forces the strings to lower case.

Additional tests added to account for situations where the search string could contain regex characters that would consequently need to be escaped. This particular test uses the regex escape function from Google Closure, which is very similar to the one available at MDN and which may be more thorough than is generally needed.

Setup

// RegExp escape function from Closure (https://github.com/google/closure-library/blob/master/closure%2Fgoog%2Fstring%2Fstring.js#L1054)
    
    var regExpEscape = function(s) {
      return String(s).replace(/([-()\[\]{}+?*.$\^|,:#<!\\])/g, '\\$1').
      replace(/\x08/g, '\\x08');
    };

Test runner

Ready to run.

Testing in
TestOps/sec
lowercased indexof - mid size
var data = "The Modern.ie scan analyzes the HTML, CSS, and JavaScript of a site or application to determine whether it is using good development practices. It warns about practices such as browser `userAgent` sniffing, use of ActiveX, or APIs specific to legacy versions of Internet Explorer."

data.toLowerCase().indexOf('Explorer'.toLowerCase());
ready
search - mid size
var data = "The Modern.ie scan analyzes the HTML, CSS, and JavaScript of a site or application to determine whether it is using good development practices. It warns about practices such as browser `userAgent` sniffing, use of ActiveX, or APIs specific to legacy versions of Internet Explorer."

data.search(/Explorer/i);
ready
lowercased indexof - small size
var data = "legacy versions of Internet Explorer."

data.toLowerCase().indexOf('Explorer'.toLowerCase());
ready
search - small size
var data = "legacy versions of Internet Explorer."

data.search(/Explorer/i);
ready
search escape - mid size
var data = "The Modern.ie scan analyzes the HTML, CSS, and JavaScript of a site or application to determine whether it is using good development practices. It warns about practices such as browser `userAgent` sniffing, use of ActiveX, or APIs specific to legacy versions of Internet Explorer."

data.search(regExpEscape('Explorer'), 'i');
ready
search escape - small size
var data = "legacy versions of Internet Explorer."

data.search(regExpEscape('Explorer'), 'i');
ready

Revisions

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