Optimized repeat and its tests

This commit is contained in:
Demian Ferreiro
2013-02-26 14:41:01 -03:00
parent fbc8417263
commit 1db89d1589
3 changed files with 29 additions and 5 deletions

View File

@@ -12,8 +12,17 @@
return literal === string.substr(string.length - len - (back || 0), len);
};
exports.repeat = function(string, n) {
return (Array(n + 1)).join(string);
exports.repeat = function(str, n) {
var res;
res = '';
while (n > 0) {
if (n & 1) {
res += str;
}
n >>>= 1;
str += str;
}
return res;
};
exports.compact = function(array) {