Skip to content

Instantly share code, notes, and snippets.

@jridgewell
Last active March 4, 2019 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jridgewell/eecce07acdcdd7a0287ca1369c4b87b4 to your computer and use it in GitHub Desktop.
Save jridgewell/eecce07acdcdd7a0287ca1369c4b87b4 to your computer and use it in GitHub Desktop.
String startsWith another string #jsbench #jsperf (https://jsbench.github.io/#eecce07acdcdd7a0287ca1369c4b87b4)
<!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>
"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