endsWith

JavaScript performance comparison

Revision 4 of this test case created

Info

Compare different endsWith implementations

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    // from https://github.com/paulmillr/es6-shim/
    function endsWith_1(searchString) {
        var endPosition = arguments[1];
        var s = String(this);
        var searchStr = searchString.toString();
        var len = s.length;
        var pos = (endPosition === undefined) ?
                len : Number.toInteger(endPosition);
        var end = Math.min(Math.max(pos, 0), len);
        var searchLength = searchString.length;
        var start = end - searchLength;
        if (start < 0) return false;
        var index = ''.indexOf.call(s, searchString, start);
        return index === start;
    }
   
   
    function endsWith_2(substring, position) {
    substring = String(substring);
   
                        var subLen = substring.length | 0;
   
                        if( !subLen )return true;//Empty string
   
                        var strLen = this.length;
   
                        if( position === void 0 )position = strLen;
                        else position = position | 0;
   
                        if( position < 1 )return false;
   
                        var fromIndex = (strLen < position ? strLen : position) - subLen;
   
                        return (fromIndex >= 0 || subLen === -fromIndex)
                                && (
                                        position === 0
                                        // if position not at the and of the string, we can optimise search substring
                                        //  by checking first symbol of substring exists in search position in current string
                                        || this.charCodeAt(fromIndex) === substring.charCodeAt(0)//fast false
                                )
                                && this.indexOf(substring, fromIndex) === fromIndex
                        ;
    }
   
   
    // from https://github.com/monolithed/ECMAScript-6/
    function endsWith_3(search, position) {
        var length = this.length;
   
                if (position === undefined || position > length) {
                        position = length;
                }
                else if (position < 0) {
                        position = 0;
                }
   
                position -= String(search).length;
   
                return position >= 0 && this.indexOf(search, position) === position;
    }
   
    var endsWith_4 = String.prototype.endsWith;
    var startsWith_4 = String.prototype.startsWith;
   
    var stringRepeat = 10;
   
    var testString_1 = "qwerty ytrewq qwerty qwerty ytrewq qwerty qwerty ytrewq qwerty qwerty ytrewq qwerty qwerty ytrewq qwerty qwerty ytrewq qwerty qwerty ytrewq qwerty qwerty ytrewq qwerty qwerty ytrewq qwerty qwerty ytrewq qwerty qwerty ytrewq qwerty ytrewq";
   
    for(var i = 0 ; i < stringRepeat  ; i++)testString_1 += testString_1;
   
    var testString_2 = "VD}M-A<O-5F&w=m1XIxK!GkpgQC&^SxQ-!tM[e9)}+TH=YXmp4QWy2[$kZPtPkS$7KK=soet,32>]n0l__XPFLbd6/Vy[|hkUZ0)f<B;kOF=IAjhhelnD0~GU_2#+3YHDq!+b]Ga-DnQyiBw!}:DW-" + "VD}M-A<O-5F&w=m1XIxK!GkpgQC&^SxQ-!tM[e9)}+TH=YXmp4QWy2[$kZPtPkS$7KK=soet,32>]n0l__XPFLbd6/Vy[|hkUZ0)f<B;kOF=IAjhhelnD0~GU_2#+3YHDq!+b]Ga-DnQyiBw!}:DW-" + "VD}M-A<O-5F&w=m1XIxK!GkpgQC&^SxQ-!tM[e9)}+TH=YXmp4QWy2[$kZPtPkS$7KK=soet,32>]n0l__XPFLbd6/Vy[|hkUZ0)f<B;kOF=IAjhhelnD0~GU_2#+3YHDq!+b]Ga-DnQyiBw!}:DW-" + "VD}M-A<O-5F&w=m1XIxK!GkpgQC&^SxQ-!tM[e9)}+TH=YXmp4QWy2[$kZPtPkS$7KK=soet,32>]n0l__XPFLbd6/Vy[|hkUZ0)f<B;kOF=IAjhhelnD0~GU_2#+3YHDq!+b]Ga-DnQyiBw!}:DW-";
   
    for(var i = 0 ; i < stringRepeat  ; i++)testString_2 += testString_2;
   
    var testString_3 = "0123456789 9874563210 0123456789 9874563210 0123456789 9874563210 0123456789 9874563210 0123456789 9874563210 0123456789 9874563210 0123456789 9874563210 0123456789 9874563210 0123456789 9874563210";
   
    for(var i = 0 ; i < stringRepeat  ; i++)testString_3 += testString_3;
   
    var testString = testString_1 + testString_2 + testString_3;
   
    Math.sign || (Math.sign = function(value) {
        var number = +value;
        if (number === 0) return number;
        if (number === NaN) return number;
        return (number < 0) ? -1 : 1;
    });
   
    Number.isFinite || (Number.isFinite = function(value) {
        return typeof value === 'number' && isFinite(value);
    });
   
    Number.toInteger || (Number.toInteger = function(value) {
        var number = +value;
        if (number === NaN) return +0;
        if (number === 0 || !Number.isFinite(number)) return number;
        return Math.sign(number) * Math.floor(Math.abs(number));
    });
};
</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
es6-shim endsWith
// true
if( endsWith_1.call(testString, testString_1.substr(0, 13), 13)
!== true )throw new Error();

if( endsWith_1.call(testString, testString_2.substr(-10, 5), testString.length - (testString_3.length + 5))
!== true )throw new Error();

if( endsWith_1.call(testString, testString_3.substr(-10, 10))
!== true )throw new Error();

// false
if( endsWith_1.call(testString, "_" + testString_1.substr(0, 13), 13)
!== false )throw new Error();

if( endsWith_1.call(testString, "_" + testString_2.substr(-10, 5), testString.length - (testString_3.length + 5))
!== false )throw new Error();

if( endsWith_1.call(testString, "_" + testString_3.substr(-10, 10))
!== false )throw new Error();
 
pending…
Termi endsWith
// true
if( endsWith_2.call(testString, testString_1.substr(0, 13), 13)
!== true )throw new Error();

if( endsWith_2.call(testString, testString_2.substr(-10, 5), testString.length - (testString_3.length + 5))
!== true )throw new Error();

if( endsWith_2.call(testString, testString_3.substr(-10, 10))
!== true )throw new Error();

// false
if( endsWith_2.call(testString, "_" + testString_1.substr(0, 13), 13)
!== false )throw new Error();

if( endsWith_2.call(testString, "_" + testString_2.substr(-10, 5), testString.length - (testString_3.length + 5))
!== false )throw new Error();

if( endsWith_2.call(testString, "_" + testString_3.substr(-10, 10))
!== false )throw new Error();
 
pending…
monolithed endsWith
// true
if( endsWith_3.call(testString, testString_1.substr(0, 13), 13)
!== true )throw new Error();

if( endsWith_3.call(testString, testString_2.substr(-10, 5), testString.length - (testString_3.length + 5))
!== true )throw new Error();

if( endsWith_3.call(testString, testString_3.substr(-10, 10))
!== true )throw new Error();

// false
if( endsWith_3.call(testString, "_" + testString_1.substr(0, 13), 13)
!== false )throw new Error();

if( endsWith_3.call(testString, "_" + testString_2.substr(-10, 5), testString.length - (testString_3.length + 5))
!== false )throw new Error();

if( endsWith_3.call(testString, "_" + testString_3.substr(-10, 10))
!== false )throw new Error();
 
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