Last active
March 4, 2019 01:27
-
-
Save jridgewell/eecce07acdcdd7a0287ca1369c4b87b4 to your computer and use it in GitHub Desktop.
String startsWith another string #jsbench #jsperf (https://jsbench.github.io/#eecce07acdcdd7a0287ca1369c4b87b4)
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>String startsWith another string #jsbench #jsperf</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
<script src="./suite.js"></script> | |
</head> | |
<body> | |
<h1>Open the console to view the results</h1> | |
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2> | |
</body> | |
</html> |
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
"use strict"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
Benchmark.prototype.setup = function () { | |
function startsWith(str, prefix) { | |
return str.startsWith(prefix); | |
} | |
function lastIndexOf(str, prefix) { | |
return str.length >= prefix.length && str.lastIndexOf(prefix, 0) === 0; | |
} | |
function slice(str, prefix) { | |
const length = prefix.length; | |
return str.length >= length && str.slice(0, length) === prefix; | |
} | |
function sliceWithoutIndex(str, prefix) { | |
return str.slice(0, prefix.length) === prefix; | |
} | |
}; | |
suite.add("String does start with, startsWith", function () { | |
// String does start with, startsWith | |
const string = 'testing#lit#'; | |
const prefix = '#lit#' | |
startsWith(string, prefix); | |
}); | |
suite.add("String does start with, lastIndexOf", function () { | |
// String does start with, lastIndexOf | |
const string = 'testing#lit#'; | |
const prefix = '#lit#' | |
lastIndexOf(string, prefix); | |
}); | |
suite.add("String does start with, slice", function () { | |
// String does start with, slice | |
const string = 'testing#lit#'; | |
const prefix = '#lit#' | |
slice(string, prefix); | |
}); | |
suite.add("String does start with, sliceWithoutIndex", function () { | |
// String does start with, slice without index check | |
const string = 'testing#lit#'; | |
const prefix = '#lit#' | |
sliceWithoutIndex(string, prefix); | |
}); | |
suite.add("String doesn't start with, startsWith", function () { | |
// String doesn't start with, startsWith | |
const string = 'testing#lit#'; | |
const prefix = 'other' | |
startsWith(string, prefix); | |
}); | |
suite.add("String doesn't start with, lastIndexOf", function () { | |
// String doesn't start with, lastIndexOf | |
const string = 'testing#lit#'; | |
const prefix = 'other' | |
lastIndexOf(string, prefix); | |
}); | |
suite.add("String doesn't start with, slice", function () { | |
// String doesn't start with, slice | |
const string = 'testing#lit#'; | |
const prefix = 'other' | |
slice(string, prefix); | |
}); | |
suite.add("String doesn't start with, sliceWithoutIndex", function () { | |
// String doesn't start with, slice without index check | |
const string = 'testing#lit#'; | |
const prefix = 'other' | |
sliceWithoutIndex(string, prefix); | |
}); | |
suite.add("Suffix longer than string, startsWith", function () { | |
// Suffix longer than string, startsWith | |
const string = '#lit#'; | |
const prefix = 'testingother' | |
startsWith(string, prefix); | |
}); | |
suite.add("Suffix longer than string, lastIndexOf", function () { | |
// Suffix longer than string, lastIndexOf | |
const string = '#lit#'; | |
const prefix = 'testingother' | |
lastIndexOf(string, prefix); | |
}); | |
suite.add("Suffix longer than string, slice", function () { | |
// Suffix longer than string, slice | |
const string = '#lit#'; | |
const prefix = 'testingother' | |
slice(string, prefix); | |
}); | |
suite.add("Suffix longer than string, sliceWithoutIndex", function () { | |
// Suffix longer than string, slice without index check | |
const string = '#lit#'; | |
const prefix = 'testingother' | |
sliceWithoutIndex(string, prefix); | |
}); | |
suite.on("cycle", function (evt) { | |
console.log(" - " + evt.target); | |
}); | |
suite.on("complete", function (evt) { | |
console.log(new Array(30).join("-")); | |
var results = evt.currentTarget.sort(function (a, b) { | |
return b.hz - a.hz; | |
}); | |
results.forEach(function (item) { | |
console.log((idx + 1) + ". " + item); | |
}); | |
}); | |
console.log("String startsWith another string #jsbench #jsperf"); | |
console.log(new Array(30).join("-")); | |
suite.run(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment