Skip to content

Instantly share code, notes, and snippets.

@leodutra
Last active October 6, 2015 19:28
Show Gist options
  • Save leodutra/3042150 to your computer and use it in GitHub Desktop.
Save leodutra/3042150 to your computer and use it in GitHub Desktop.
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