Performance improvements to String#times.
This commit is contained in:
parent
db371ddb5a
commit
d195111a9d
@ -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ć]
|
||||
|
@ -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() {
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user