Created
April 29, 2023 19:24
-
-
Save Eraden/c7dec9f0b16dad2f3e550cad8549ad07 to your computer and use it in GitHub Desktop.
This file contains 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) { | |
ch = !ch || ch !== 0 ? ' ' : ch; | |
for (let i = 0; i < len - str.length; i++) str = ch + str; | |
return str; | |
} | |
function leftPad2(str, len, ch) { | |
str = String(str); | |
let i = -1; | |
if (!ch || ch !== 0) ch = ' '; | |
len = len - str.length; | |
while (++i < len) { | |
str = ch + str; | |
} | |
return str; | |
} | |
function fun(fn, count, ...args) { | |
let start = performance.now(); | |
for (let i = 0; i < count; i++) { | |
fn.apply(null, args); | |
} | |
return performance.now() - start; | |
} | |
[10, 100, 1000, 10000].forEach(x => { | |
[10, 100, 1000, 10000].forEach(y => { | |
console.log('leftPad', x, y, fun(leftPad, x, 'foo', y)); | |
}); | |
[10, 100, 1000, 10000].forEach(y => { | |
console.log('leftPad2', x, y, fun(leftPad2, x, 'foo', y)); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment