Performance improvements to String#times.

This commit is contained in:
Tobie Langel 2007-10-13 19:05:19 +00:00
parent db371ddb5a
commit d195111a9d
3 changed files with 20 additions and 3 deletions

View File

@ -1,5 +1,7 @@
*SVN*
* Performance improvements to String#times. [Martin Ström]
* Make Ajax.Response#getHeaderJSON and Ajax.Response#getResponseJSON pseudo private instance methods. [Tobie Langel]
* Make ObjectRange use the new Class.create syntax. [Mislav Marohnić]

View File

@ -119,9 +119,7 @@ Object.extend(String.prototype, {
},
times: function(count) {
var result = '';
for (var i = 0; i < count; i++) result += this;
return result;
return count < 1 ? '' : new Array(count + 1).join(this);
},
camelize: function() {

View File

@ -463,13 +463,30 @@
}},
testTimes: function() {with(this) {
assertEqual('', ''.times(0));
assertEqual('', ''.times(5));
assertEqual('', 'a'.times(-1));
assertEqual('', 'a'.times(0));
assertEqual('a', 'a'.times(1));
assertEqual('aa', 'a'.times(2));
assertEqual('aaaaa', 'a'.times(5));
assertEqual('foofoofoofoofoo', 'foo'.times(5));
assertEqual('', 'foo'.times(-5));
/*window.String.prototype.oldTimes = function(count) {
var result = '';
for (var i = 0; i < count; i++) result += this;
return result;
};
benchmark(function() {
'foo'.times(15);
}, 1000, 'new: ');
benchmark(function() {
'foo'.oldTimes(15);
}, 1000, 'previous: ');*/
}},
testToJSON: function() {with(this) {