How's this:
function leftpad (str, len, ch) {
ch = (len -= str.length) <= 0 ? '' : !ch && ch !== 0 ? ' ' : String(ch);
while (--len > 0) ch += ch[0];
return ch + String(str);
}
No local variables, less manipulation of the input string, the string grows at the tail which is more efficient, and the code is much shorter.(With a bit of work you can use the longer ch string that is built to reduce the number of string appends even more by appending multiple characters at once. Although probably not worth it for this function.)