Skip to content

Instantly share code, notes, and snippets.

@leodutra
Last active Oct 6, 2015
Embed
What would you like to do?
Very fast left pad, anything (JavaScript)
leftPad = function leftPad(value, size, pad) { // very very fast
if (value.length < size) {
size -= value.length;
var res = '';
for(;;) {
if (size & 1) res += pad;
size >>= 1;
if (size) pad += pad;
else break;
}
return res + value;
}
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment