Skip to content

Instantly share code, notes, and snippets.

@jridgewell
Created January 23, 2020 03:46
Show Gist options
  • Select an option

  • Save jridgewell/31bf76ea5fa539b60fb1b80b321916d3 to your computer and use it in GitHub Desktop.

Select an option

Save jridgewell/31bf76ea5fa539b60fb1b80b321916d3 to your computer and use it in GitHub Desktop.
Searching strings for any of two special chars (https://jsbench.github.io/#31bf76ea5fa539b60fb1b80b321916d3) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Searching strings for any of two special chars</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 () {
const shortNoMatch = 'test';
const shortMatch = 'te>t';
const mediumNoMatch = 'test'.repeat(20);
const mediumMatch = 'te'.repeat(20) + '>' + 'st'.repeat(20).slice(1);
const longNoMatch = 'test'.repeat(100);
const longMatch = 'te'.repeat(100) + '>' + 'st'.repeat(100).slice(1);
function indexOfSearch(str) {
const rightCurly = str.indexOf('}');
const greaterThan = str.indexOf('>');
if (rightCurly === -1 || greaterThan === -1) return '';
const index = Math.min(rightCurly, greaterThan);
return str[index];
}
const regex = /}|>/;
function regexSearch(str) {
const match = regex.exec(str);
return match ? match[0] : '';
}
function loopSearch(str) {
for (let i = 0; i < str.length; i++) {
const c = str[i];
if (c === '}' || c === '>') return c;
}
return '';
}
};
suite.add("Short, no match, for loop search", function () {
// Short, no match, for loop search
loopSearch(shortNoMatch);
});
suite.add("Short, no match, regex search", function () {
// Short, no match, regex search
regexSearch(shortNoMatch);
});
suite.add("Short, no match, indexOf search", function () {
// Short, no match, indexOf search
indexOfSearch(shortNoMatch);
});
suite.add("Short, match, for loop search", function () {
// Short, match, for loop search
loopSearch(shortMatch);
});
suite.add("Short, match, regex search", function () {
// Short, match, regex search
regexSearch(shortMatch);
});
suite.add("Short, match, indexOf search", function () {
// Short, match, indexOf search
indexOfSearch(shortMatch);
});
suite.add("Medium, no match, for loop search", function () {
// Medium, no match, for loop search
loopSearch(mediumNoMatch);
});
suite.add("Medium, no match, regex search", function () {
// Medium, no match, regex search
regexSearch(mediumNoMatch);
});
suite.add("Medium, no match, indexOf search", function () {
// Medium, no match, indexOf search
indexOfSearch(mediumNoMatch);
});
suite.add("Medium, match, for loop search", function () {
// Medium, match, for loop search
loopSearch(mediumMatch);
});
suite.add("Medium, match, regex search", function () {
// Medium, match, regex search
regexSearch(mediumMatch);
});
suite.add("Medium, match, indexOf search", function () {
// Medium, match, indexOf search
indexOfSearch(mediumMatch);
});
suite.add("Medium, no match, for loop search", function () {
// Medium, no match, for loop search
loopSearch(longNoMatch);
});
suite.add("Medium, no match, regex search", function () {
// Medium, no match, regex search
regexSearch(longNoMatch);
});
suite.add("Medium, no match, indexOf search", function () {
// Medium, no match, indexOf search
indexOfSearch(longNoMatch);
});
suite.add("Medium, match, for loop search", function () {
// Medium, match, for loop search
loopSearch(longMatch);
});
suite.add("Medium, match, regex search", function () {
// Medium, match, regex search
regexSearch(longMatch);
});
suite.add("Medium, match, indexOf search", function () {
// Medium, match, indexOf search
indexOfSearch(longMatch);
});
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("Searching strings for any of two special chars");
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