-
Type:
Improvement
-
Resolution: Won't Fix
-
Priority:
Trivial - P5
-
None
-
Affects Version/s: None
-
Component/s: JavaScript, Shell
-
None
-
None
-
None
-
None
-
None
-
None
-
None
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);
}());
- links to