regex vs toLowerCase() (v4)

Revision 4 of this benchmark created on


Description

perf difference between running a string through .toLowerCase() before applying a regex or just doing it in the regex.

Setup

var testString = "iPhone";

Test runner

Ready to run.

Testing in
TestOps/sec
regex
var r = /PH/i;

var match = ~testString.search(r);
ready
.toLowerCase() first
var r = "PH";
var match = ~testString.toLowerCase().indexOf(r.toLowerCase());
ready
escaped regex
var r = "PH";

var regex = RegExp(r.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), "i");

var match = ~testString.search(regex);
ready

Revisions

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