Created
March 29, 2019 06:22
-
-
Save robinrodricks/a71b4b38ce8b7a2c3c52e76493a2700c to your computer and use it in GitHub Desktop.
JS startsWith/endsWith Speedtest
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
<script> | |
var s = "HELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLOHELLO"; | |
var p = "HE"; | |
var t = performance.now(); | |
for(i=0;i<100000;i++){ | |
if (s.substring(0, 2) == p){ | |
} | |
} | |
console.log('startsWith using Substring : ' + (performance.now() - t) + " ms") | |
var t = performance.now(); | |
for(i=0;i<100000;i++){ | |
if (s.lastIndexOf(p, 0) == 0){ | |
} | |
} | |
console.log('startsWith using LastIndexOf : ' + (performance.now() - t) + " ms") | |
var t = performance.now(); | |
for(i=0;i<100000;i++){ | |
var elen = p.length; | |
var slen = s.length; | |
if (slen >= elen && s.substr(slen - elen, elen) == p){ | |
} | |
} | |
console.log('endsWith using Substring : ' + (performance.now() - t) + " ms") | |
var t = performance.now(); | |
for(i=0;i<100000;i++){ | |
var elen = p.length; | |
var slen = s.length; | |
if (s.indexOf(p, (slen - elen)) == (slen - elen)){ | |
} | |
} | |
console.log('endsWith using IndexOf : ' + (performance.now() - t) + " ms") | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment