Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-19370

String.endsWith() shouldn't use regexp (even escaped)

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.1.7
    • Affects Version/s: None
    • Component/s: JavaScript
    • None
    • Fully Compatible
    • Build 7 08/10/15

      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;
      };
      

            Assignee:
            jonathan.reams@mongodb.com Jonathan Reams
            Reporter:
            kevin.pulo@mongodb.com Kevin Pulo
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: