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

String.prototype.endsWith performance increase

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Won't Fix
    • Icon: Trivial - P5 Trivial - P5
    • None
    • None
    • JavaScript, Shell

    Description

      String.prototype.endsWith() uses unnecessary RegExp and affects performance.

      $ time mongo test newEndsWithTest.js 
      MongoDB shell version: 2.2.2
      connecting to: test
      1000000
       
      real	0m0.735s
      user	0m0.688s
      sys	0m0.048s
       
       
      $ time mongo test endsWithTest.js 
      MongoDB shell version: 2.2.2
      connecting to: test
      Mon Feb 25 17:04:41 out of memory src/mongo/shell/utils.js:348
      failed to load: endsWithTest.js
       
      real	0m2.666s
      user	0m2.576s
      sys	0m0.084s

      newEndsWith.js

      (function() {
          var endsWith = function(x, y) {
              var i = x.lastIndexOf(y);
              return i > -1 && i === (x.length - y.length);
          };
       
          var count = 0;
          for (var i = 0; i < 1000000; i++) {
              if (endsWith(i + "hola", "ola")) {
                  count++;
              }
          }
          print(count);
      }());

      endsWithTest.js

      (function() {
          var count = 0;
          for (var i = 0; i < 1000000; i++) {
              if ((i+"hola").endsWith("ola")) {
                  count++;
              }
          }
          print(count);
      }());

      Attachments

        Activity

          People

            andrew.emil@10gen.com Andrew Emil (Inactive)
            saml saml
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: