jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
Perf-testing a JS startsWith() function
<script>
// Methods to test
// String.prototype.startsWith1 using ===
if (!String.prototype.startsWith1) {
String.prototype.startsWith1 = function(str) {
return (this.indexOf(str) === 0);
}
}
// String.prototype.startsWith using !
if (!String.prototype.startsWith2) {
String.prototype.startsWith2 = function(str) {
return !this.indexOf(str);
}
}
// Using substr
var subStringChk = function(data, str) {
return data.substr(0, str.length) === str;
}
// Using lastIndexOf === 0
var lastIndexChk = function(data, str) {
return data.lastIndexOf(str, 0) === 0;
}
// Using firstIndexOf === 0
var firstIndexChk = function(data, str) {
return data.indexOf(str, 0) === 0;
}
// sample data
var origString = 'hello, world';
var testString = 'he';
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
String.prototype.startsWith1 |
| ready |
String.prototype.startsWith2 |
| ready |
subStringChk |
| ready |
lastIndexChk |
| ready |
firstIndexChk |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.