Skip to content

Instantly share code, notes, and snippets.

@joshpowlison
Last active January 17, 2020 10:56
Show Gist options
  • Save joshpowlison/8a0c2f5baba00c4b49e4008f70e91228 to your computer and use it in GitHub Desktop.
Save joshpowlison/8a0c2f5baba00c4b49e4008f70e91228 to your computer and use it in GitHub Desktop.
Large sets of .indexOf run slower than regex .test? #jsbench #jsperf (http://jsbench.github.io/#8a0c2f5baba00c4b49e4008f70e91228) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Large sets of .indexOf run slower than regex .test? #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 () {
var input="Test text hours";
var potato;
};
suite.add("input=input.toLowerCase();", function () {
input=input.toLowerCase();
//Get the type
if(input.indexOf('name')!==-1) potato="name";
else if(input.indexOf('date')!==-1) potato="date";
else if(input.indexOf('percent')!==-1 || input.indexOf('%')!==-1) potato="percent";
else if(input.indexOf('hour')!==-1) potato="hours";
else if(input.indexOf('min')!==-1) potato="minutes";
else if(input.indexOf('sec')!==-1) potato="seconds";
});
suite.add("input=input.toLowerCase();", function () {
input=input.toLowerCase();
//Get the type
if(input.indexOf('name')!==-1) input="name";
else if(input.indexOf('date')!==-1) input="date";
else if(input.indexOf('percent')!==-1 || input.indexOf('%')!==-1) input="percent";
else if(input.indexOf('hour')!==-1) input="hours";
else if(input.indexOf('min')!==-1) input="minutes";
else if(input.indexOf('sec')!==-1) input="seconds";
});
suite.add("Get the type", function () {
//Get the type
if(/name|title/i.test(input)) potato="name";
else if(/date|release/i.test(input)) potato="date";
else if(/%|percent/i.test(input)) potato="percent";
else if(/hour/i.test(input)) potato="hours";
else if(/min/i.test(input)) potato="minutes";
else if(/sec/i.test(input)) potato="seconds";
});
suite.add("Get the type", function () {
//Get the type
if(/name|title/i.test(input)) input="name";
else if(/date|release/i.test(input)) input="date";
else if(/%|percent/i.test(input)) input="percent";
else if(/hour/i.test(input)) input="hours";
else if(/min/i.test(input)) input="minutes";
else if(/sec/i.test(input)) input="seconds";
});
suite.add("NO LOWER CASE", function () {
//NO LOWER CASE
//Get the type
if(input.indexOf('name')!==-1) input="name";
else if(input.indexOf('date')!==-1) input="date";
else if(input.indexOf('percent')!==-1 || input.indexOf('%')!==-1) input="percent";
else if(input.indexOf('hour')!==-1) input="hours";
else if(input.indexOf('min')!==-1) input="minutes";
else if(input.indexOf('sec')!==-1) input="seconds";
});
suite.add("NO LOWER CASE", function () {
//NO LOWER CASE
//Get the type
if(/name|title/.test(input)) input="name";
else if(/date|release/.test(input)) input="date";
else if(/%|percent/.test(input)) input="percent";
else if(/hour/.test(input)) input="hours";
else if(/min/.test(input)) input="minutes";
else if(/sec/.test(input)) input="seconds";
});
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("Large sets of .indexOf run slower than regex .test? #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