Skip to content

Instantly share code, notes, and snippets.

@arccoza
Created December 4, 2017 05:50
Show Gist options
  • Save arccoza/9f7f6df6b0e90fc2e1a3b94384e5b2dc to your computer and use it in GitHub Desktop.
Save arccoza/9f7f6df6b0e90fc2e1a3b94384e5b2dc to your computer and use it in GitHub Desktop.
String / RegExp operations (http://jsbench.github.io/#9f7f6df6b0e90fc2e1a3b94384e5b2dc) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>String / RegExp operations</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 str = 'asdfkjroi_vnarevihg_asdfasdfg';
var arr = ['asdfkjroi', 'vnarevihg', 'asdfasdfg'];
};
suite.add("var idx = str.indexOf('_');", function () {
var idx = str.indexOf('_');
});
suite.add("var idx = str.search(/_/);", function () {
var idx = str.search(/_/);
});
suite.add("var idx = str.search('_');", function () {
var idx = str.search('_');
});
suite.add("var arr = str.split('_');", function () {
var arr = str.split('_');
});
suite.add("var arr = str.split(/_/)", function () {
var arr = str.split(/_/)
});
suite.add("var str = arr.join('_');", function () {
var str = arr.join('_');
});
suite.add("/_/.exec(str);", function () {
/_/.exec(str);
});
suite.add("str.match(/_/);", function () {
str.match(/_/);
});
suite.add("str.substring(9, 15);", function () {
str.substring(9, 15);
});
suite.add("str.slice(9, 15);", function () {
str.slice(9, 15);
});
suite.add("str.slice(-15, -9);", function () {
str.slice(-15, -9);
});
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 / RegExp operations");
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