Skip to content

Instantly share code, notes, and snippets.

@robinrodricks
Created March 29, 2019 06:22
Show Gist options
  • Save robinrodricks/a71b4b38ce8b7a2c3c52e76493a2700c to your computer and use it in GitHub Desktop.
Save robinrodricks/a71b4b38ce8b7a2c3c52e76493a2700c to your computer and use it in GitHub Desktop.
JS startsWith/endsWith Speedtest
<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