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

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

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • 3.1.7
    • None
    • JavaScript
    • None
    • Fully Compatible
    • Build 7 08/10/15

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

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: