Last active
December 3, 2022 07:27
-
-
Save JobLeonard/9cb819bf1ce429575f8535a211f72d5a to your computer and use it in GitHub Desktop.
split string into segments: regex vs substr vs split/slice/join (plus plain split for comparison) (http://jsbench.github.io/#9cb819bf1ce429575f8535a211f72d5a) #jsbench #jsperf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>split string into segments: regex vs substr vs split/slice/join (plus plain split for comparison)</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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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 = 'abcdefghijkl'; | |
for (var i = 0; i < 8; i++){ | |
str += str; | |
} | |
var rExp = /.{1,3}/g; | |
function splitChunks(_str, n){ | |
var chunks = []; | |
for (var i = 0, charsLength = _str.length; i < charsLength; i += n) { | |
chunks.push(_str.substring(i, i + n)); | |
} | |
return chunks; | |
} | |
}; | |
suite.add("comparison to str.split('');", function () { | |
// comparison to str.split(''); | |
var chunks = str.split(''); | |
}); | |
suite.add("str.match(/.{1,3}/g);", function () { | |
// str.match(/.{1,3}/g); | |
var chunks = str.match(/.{1,3}/g); | |
}); | |
suite.add("str.match(rExp); , where rExp = /.{1,3}/g; (perhaps initialising the regex causes overhead)", function () { | |
// str.match(rExp); , where rExp = /.{1,3}/g; (perhaps initialising the regex causes overhead) | |
var chunks = str.match(rExp); | |
}); | |
suite.add("substring + array push", function () { | |
// substring + array push | |
var chunks = []; | |
for (var i = 0, charsLength = str.length; i < charsLength; i += 3) { | |
chunks.push(str.substring(i, i + 3)); | |
} | |
}); | |
suite.add("substring + array push as function", function () { | |
// substring + array push as function | |
chunks = splitChunks(str, 3); | |
}); | |
suite.add("split + slice + join", function () { | |
// split + slice + join | |
var _chunks = str.split(''); | |
var chunks = []; | |
for (var i = 0, charsLength = str.length; i < charsLength; i += 3) { | |
chunks.push(_chunks.slice(i, i + 3).join('')); | |
} | |
}); | |
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("split string into segments: regex vs substr vs split/slice/join (plus plain split for comparison)"); | |
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