3 ms·
module.exports = leftpad; function leftpad (str, len, ch) { str = String(str); var i = -1; if (!ch && ch !== 0) ch = ' '; len = len - str
by venomsnake 11y ago
module.exports = leftpad;
function leftpad (str, len, ch) {
str = String(str);
var i = -1;
if (!ch && ch !== 0) ch = ' ';
len = len - str.length;
while (++i < len) {
str = ch + str;
}
return str;
}
Isn't that the least efficient way to do that function? Prepending a string has always been very expensive operation.
Calculating needed length. Using repeat and just concatenating 2 strings would be faster.
- titzer 11y agoV8 internally uses cons-style strings for these operations, and only flattens the string when necessary for other operations.