Skip to content

Instantly share code, notes, and snippets.

@brianchung808
Last active May 12, 2021 00:31
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 brianchung808/b57b39d65c0a0301dbbdae33f1bbe7ed to your computer and use it in GitHub Desktop.
Save brianchung808/b57b39d65c0a0301dbbdae33f1bbe7ed to your computer and use it in GitHub Desktop.
includes vs. fuzzy (https://jsbench.github.io/#b57b39d65c0a0301dbbdae33f1bbe7ed) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>includes vs. fuzzy</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 includes(search, target) {
const start = target.toLowerCase().indexOf(search);
if (start < 0) return null;
return [
{
start,
end: start + search.length
}
];
}
function fuzzy(search, target) {
// if same length, short circuit to includes
if (search.length === target.length) return includes(search, target);
if (search.length === 0 || search.length > target.length) return [];
if (search.length > target.length) return [];
return fuzzyHelper(search.toLowerCase(), target.toLowerCase(), 0, 0);
}
function fuzzyHelper(search, target, i, j) {
if (i === search.length) {
return [];
} else if (j === target.length) {
return null;
} else {
if (search[i] === target[j]) {
// if we have a match, increment both search/target pointers
// and keep searching
const result = fuzzyHelper(search, target, i + 1, j + 1);
return result ? join({start: j, end: j + 1}, result) : null;
}
// if there's no match, check the next char of the target
return fuzzyHelper(search, target, i, j + 1);
}
}
function join(head, tail) {
if (tail.length === 0) {
tail = [head];
} else if (head.end === tail[0].start) {
tail[0].start = head.start;
} else {
tail.unshift(head);
}
return tail;
}
};
suite.add("includes(\"hey\", \"hey-there\");", function () {
includes("hey", "hey-there");
});
suite.add("fuzzy(\"hey\", \"hey-there\");", function () {
fuzzy("hey", "hey-there");
});
suite.add("includes(\"\", \"hey-there\");", function () {
includes("", "hey-there");
});
suite.add("fuzzy(\"\", \"hey-there\");", function () {
fuzzy("", "hey-there");
});
suite.add("includes(\"hey-there\", \"hey-there\");", function () {
includes("hey-there", "hey-there");
});
suite.add("fuzzy(\"hey-there\", \"hey-there\");", function () {
fuzzy("hey-there", "hey-there");
});
suite.add("includes(\"y-th\", \"hey-there\");", function () {
includes("y-th", "hey-there");
});
suite.add("fuzzy(\"y-th\", \"hey-there\");", function () {
fuzzy("y-th", "hey-there");
});
suite.add("includes(\"there-he\", \"hey-there\");", function () {
includes("there-he", "hey-there");
});
suite.add("fuzzy(\"zzz zzzz\", \"hey-there\");", function () {
fuzzy("zzz zzzz", "hey-there");
});
suite.add("includes(\"ere\", \"hey-there\");", function () {
includes("ere", "hey-there");
});
suite.add("fuzzy(\"ere\", \"hey-there\");", function () {
fuzzy("ere", "hey-there");
});
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("includes vs. fuzzy");
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