String compare Regex V IndexOf

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var validCharacters = "abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789";
   
    var regExMatch = new RegExp(/[^-A-Za-z0-9_]/gi);
   
    var shortValidString = "Hello";
   
    var longValidString = "abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789";
   
    var shortInvalidString = "SFD*@611";
   
    var longInvalidString = "abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQR#$@VWXYZ_0123456789abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQR)&#_@@YZ_0123456789abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWX(*&@#$3456789abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789";
};
</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
short Valid IndexOf
var newString = "";

var originalString = shortValidString ;

for (var c = 0, len = originalString.length; c < len; c++) {
   if (validCharacters.indexOf(originalString.charAt(c)) != -1) {
      newString = newString + originalString.charAt(c).toString();
   }
}
pending…
short Invalid IndexOf
var newString = "";

var originalString = shortInvalidString;

for (var c = 0, len = originalString.length; c < len; c++) {
   if (validCharacters.indexOf(originalString.charAt(c)) != -1) {
      newString = newString + originalString.charAt(c).toString();
   }
}
pending…
long Valid IndexOf
var newString = "";

var originalString = longValidString;

for (var c = 0, len = originalString.length; c < len; c++) {
   if (validCharacters.indexOf(originalString.charAt(c)) != -1) {
      newString = newString + originalString.charAt(c).toString();
   }
}
pending…
long Invalid IndexOf
var newString = "";

var originalString = longInvalidString;

for (var c = 0, len = originalString.length; c < len; c++) {
   if (validCharacters.indexOf(originalString.charAt(c)) != -1) {
      newString = newString + originalString.charAt(c).toString();
   }
}
pending…
short Valid Regex
var desired = shortValidString.replace(regExMatch, '');
pending…
short Invalid Regex
var desired = shortInvalidString.replace(regExMatch, '');
pending…
long Valid Regex
var desired = longValidString.replace(regExMatch, '');
pending…
long Invalid Regex
var desired = longInvalidString.replace(regExMatch, '');
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