[SERVER-19370] String.endsWith() shouldn't use regexp (even escaped) Created: 13/Jul/15  Updated: 19/Sep/15  Resolved: 30/Jul/15

Status: Closed
Project: Core Server
Component/s: JavaScript
Affects Version/s: None
Fix Version/s: 3.1.7

Type: Improvement Priority: Major - P3
Reporter: Kevin Pulo Assignee: Jonathan Reams
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-19369 Add String.includes() polyfill Closed
Backwards Compatibility: Fully Compatible
Sprint: Build 7 08/10/15
Participants:

 Description   

The implementation of String.endsWith() needlessly uses regexps:

String.prototype.endsWith = function(str){
    return new RegExp(RegExp.escape(str) + "$").test(this)
}

Safer and almost certainly faster is to use indexOf() to just directly check the end of the string:

String.prototype.endsWith = function(suffix) {
    return this.indexOf(suffix, this.length - suffix.length) !== -1;
};



 Comments   
Comment by Githook User [ 30/Jul/15 ]

Author:

{u'username': u'jbreams', u'name': u'Jonathan Reams', u'email': u'jbreams@mongodb.com'}

Message: SERVER-19370 Shell function String.endsWith shouldn't use regexp
Branch: master
https://github.com/mongodb/mongo/commit/6fc93e80be7aa219019681ff212af953f9543205

Generated at Thu Feb 08 03:50:44 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.