Skip to content

Commit

Permalink
Fast String.prototype.repeat
Browse files Browse the repository at this point in the history
  • Loading branch information
termi committed Oct 3, 2012
1 parent 3147b81 commit b094765
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions lib/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -753,17 +753,23 @@
*
* 'jumpy'.repeat(2) -> 'jumpyjumpy'
* 'a'.repeat(5) -> 'aaaaa'
* 'a'.repeat(0) -> ''
*
***/
'repeat': function(num) {
var str = '', i = 0;
if(isNumber(num) && num > 0) {
while(i < num) {
str += this;
i++;
}
if(!isNumber(num) || num < 1)return "";

var result = '', self = this;

while (num) {
if (num & 1)
result += self;

if (num >>= 1)
self += self;
}
return str;

return result;
},

/***
Expand Down Expand Up @@ -851,7 +857,6 @@
});
return context;
}

});


Expand Down

0 comments on commit b094765

Please sign in to comment.