Last active
May 17, 2023 01:54
-
-
Save tallpeak/b6455e02100ca929d297c1ad118c5a59 to your computer and use it in GitHub Desktop.
because of Primagen https://www.youtube.com/watch?v=NmHUjxKpD90
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function leftPad(str, len, ch) { | |
str = String(str); | |
var i=-1; | |
if (!ch && ch != 0) ch=' '; | |
len -= str.length; | |
while (++i<len) { | |
str = ch + str; | |
} | |
return str; | |
} | |
function leftPadP(str,len,ch) { | |
return new Array(len-str.length).fill(!ch&&ch!=0?' ':ch).join("")+str | |
} | |
function leftPadP2(str,len,ch) { | |
return new Array(len-str.length).join(!ch&&ch!=0?' ':ch)+str | |
} | |
new Array(42).join(' ') | |
function leftPadA(str, len, ch) { | |
s = ""; | |
var i=-1; | |
if (!ch && ch != 0) ch=' '; | |
len -= str.length; | |
while (++i<len) { | |
s += ch; | |
} | |
return s + String(str); | |
} | |
function leftPadA2(str, len, ch) | |
{ | |
return str.padStart(len, !ch&&ch!=0?' ':ch); | |
} | |
function run(fn, count, ...args) { | |
const start = performance.now(); | |
for (i=0;i<count;++i) { | |
ret = fn.apply(null,args); | |
//console.log(ret); | |
} | |
return performance.now()-start; | |
} | |
console.log("loops","spaces","leftPad(orig)","Prime ", "Prime2 ", "Aaron ","padStart"); | |
[100,1000].forEach(x=> { | |
[100,1000,10000,100000].forEach(y=> { | |
console.log(x.toString().padStart(5),y.toString().padStart(6), | |
run(leftPad,x,'foo',y).toFixed(3).padStart(9), | |
run(leftPadP,x,'foo',y).toFixed(3).padStart(9), | |
run(leftPadP2,x,'foo',y).toFixed(3).padStart(9), | |
run(leftPadA,x,'foo',y).toFixed(3).padStart(9), | |
run(leftPadA2,x,'foo',y).toFixed(3).padStart(9), | |
); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.