[SERVER-8723] String.prototype.endsWith performance increase Created: 25/Feb/13  Updated: 10/Dec/14  Resolved: 22/Apr/13

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

Type: Improvement Priority: Trivial - P5
Reporter: saml Assignee: Andrew Emil (Inactive)
Resolution: Won't Fix Votes: 0
Labels: neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File endWithTest2.js    
Participants:

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



 Comments   
Comment by Andrew Emil (Inactive) [ 15/Apr/13 ]

Hey SamL, I was going looking over this ticket, let me know or respond here if you would like to submit some tests or changes for this pull request. I will check back next week and, if there is no activity, close it out.

Comment by Andrew Emil (Inactive) [ 13/Mar/13 ]

Using the file I uploaded to test this change, it seems that we do not get a performance increase using lastIndexOf instead of regular expressions. Here is the output when I run endWithTest2.js

$ mongo endWithTest2.js
MongoDB shell version: 2.3.3-pre-
connecting to: test
1000000
ew: 1244
1000000
ew2: 2200

Based on these results I would say that this change should not go forward.

Comment by Andrew Emil (Inactive) [ 13/Mar/13 ]

File to test request (credit to Ben Becker)

Comment by Daniel Pasette (Inactive) [ 26/Feb/13 ]

See: https://github.com/mongodb/mongo/pull/386

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