endsWith

Benchmark created by MONTILLET Xavier on


Preparation HTML

<script>
  var container = 'qwertyuiop';
  var trues = ['', 'p', 'op', 'qwertyuiop'];
  var falses = [' ', 'po', '1qwertyuiop'];
  
  function test(endsWith) {
    var i = 99999;
    while (i--) {
      trues.forEach(function(s) {
        if (!endsWith.call(container, s)) {
          throw new Error(s + ' FAILS!');
        }
      });
      falses.forEach(function(s) {
        if (endsWith.call(container, s)) {
          throw new Error(s + ' FAILS!');
        }
      });
    }
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
substring
test(function(s) {
  return this.substring(this.length - s.length) === String(s);
});
ready
lastIndexOf
test(function(s) {
  var t = String(s);
  var index = this.lastIndexOf(t)
  return index >= 0 && index === this.length - t.length;
});
ready
"hard"
test(function(s) {
  s = String(s);
  var i = s.length;
  var thisStartIndex = this.length - i;
  while (i--) {
    if (s.charAt(i) !== this.charAt(thisStartIndex + i)) {
      return false;
    }
  }
  return true;
});
ready

Revisions

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

  • Revision 1: published by MONTILLET Xavier on