Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36
Chrome 106
Mac OS X 10.15.7
Desktop
2 years ago
Test name Executions per second
regex start 382.9 Ops/sec
startsWith 851.6 Ops/sec
regex end 356.8 Ops/sec
endsWith 552.1 Ops/sec
indexOf start 4793.2 Ops/sec
indexOf end 4779.5 Ops/sec
charAt end 631.6 Ops/sec
charCodeAt end 745.8 Ops/sec
charCodeAt start 745.2 Ops/sec
HTML Preparation code:
AخA
 
1
<div></div>
Script Preparation code:
x
 
window.regexStart = /^test/;
window.regexEnd = /test$/;
window.match = 'test';
window.matchLength = window.match.length;
var data = window.data = [];
const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var TOTAL_STRINGS = window.TOTAL_STRINGS = 100000;
function getRandomInt(max) {
  return Math.floor(Math.random() * max);
}
function makeRandomString(len) {
  var text = "";
  for( var i=0; i < len; i++ ) {
    text += possible.charAt(getRandomInt(possible.length));
  }
  return text;
}
while (data.length < TOTAL_STRINGS) {
  data.push(makeRandomString(getRandomInt(20)));
}
Tests:
  • regex start

     
    var x = 0;
    var TOTAL_STRINGS = window.TOTAL_STRINGS;
    var data = window.data;
    var regex = window.regexStart;
    while (x < TOTAL_STRINGS) {
      const str = data[x];
      regex.test(str);
      x += 1;
    }
  • startsWith

     
    var x = 0;
    var TOTAL_STRINGS = window.TOTAL_STRINGS;
    var data = window.data;
    var match = window.match;
    while (x < TOTAL_STRINGS) {
      const str = data[x];
      str.startsWith(match) === 0;
      x += 1;
    }
  • regex end

     
    var x = 0;
    var TOTAL_STRINGS = window.TOTAL_STRINGS;
    var data = window.data;
    var regex = window.regexEnd;
    while (x < TOTAL_STRINGS) {
      const str = data[x];
      regex.test(str);
      x += 1;
    }
  • endsWith

     
    var x = 0;
    var TOTAL_STRINGS = window.TOTAL_STRINGS;
    var data = window.data;
    var match = window.match;
    while (x < TOTAL_STRINGS) {
      const str = data[x];
      str.endsWith(match) === 0;
      x += 1;
    }
  • indexOf start

     
    var x = 0;
    var TOTAL_STRINGS = window.TOTAL_STRINGS;
    var data = window.data;
    var match = window.match;
    while (x < TOTAL_STRINGS) {
      const str = data[x];
      str.indexOf(match) === 0;
      x += 1;
    }
  • indexOf end

     
    var x = 0;
    var TOTAL_STRINGS = window.TOTAL_STRINGS;
    var data = window.data;
    var match = window.match;
    var length = window.matchLength;
    while (x < TOTAL_STRINGS) {
      const str = data[x];
      str.indexOf(match) === str.length - length;
      x += 1;
    }
  • charAt end

     
    var x = 0;
    var TOTAL_STRINGS = window.TOTAL_STRINGS;
    var data = window.data;
    var match = window.match;
    var matchLen = match.length;
    while (x < TOTAL_STRINGS) {
      const str = data[x];
      if (str && str.length >= matchLen) {
        let strLen = str.length;
        for (let i = 1; i <= matchLen; i++) {
            if (str.charAt(strLen - i) !== match.charAt(matchLen - i))
              break;
        }
      }
      x += 1;
    }
  • charCodeAt end

     
    var x = 0;
    var TOTAL_STRINGS = window.TOTAL_STRINGS;
    var data = window.data;
    var match = window.match;
    var matchLen = match.length;
    while (x < TOTAL_STRINGS) {
      const str = data[x];
      if (str && str.length >= matchLen) {
        let strLen = str.length;
        for (let i = 1; i <= matchLen; i++) {
            if (str.charCodeAt(strLen - i) !== match.charCodeAt(matchLen - i))
              break;
        }
      }
      x += 1;
    }
  • charCodeAt start

     
    var x = 0;
    var TOTAL_STRINGS = window.TOTAL_STRINGS;
    var data = window.data;
    var match = window.match;
    var matchLen = match.length;
    while (x < TOTAL_STRINGS) {
      const str = data[x];
      if (str && str.length >= matchLen) {
        for (let i = 0; i < matchLen; i++) {
            if (str.charCodeAt(i) !== match.charCodeAt(i))
              break;
        }
      }
      x += 1;
    }