Skip to content

Instantly share code, notes, and snippets.

@leodutra
Last active October 6, 2015 19:28
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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