Last active
March 27, 2017 12:52
-
-
Save JobLeonard/d70fc60aeaed28f321133c4c840e6ad6 to your computer and use it in GitHub Desktop.
split string various lengths (http://jsbench.github.io/#d70fc60aeaed28f321133c4c840e6ad6) #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 various lengths</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; | |
} | |
function splitChunks(_str, n){ | |
var size = Math.ceil(_str.length / n), | |
chunks = new Array(size), | |
offset = 0, | |
i = 0; | |
while (i < size){ | |
chunks[i++] = _str.substring(offset, offset + n); | |
offset += n; | |
} | |
return chunks; | |
} | |
}; | |
suite.add("str.split('') for comparison", function () { | |
// str.split('') for comparison | |
var chunks = str.split(''); | |
}); | |
suite.add("1 length substring", function () { | |
// 1 length substring | |
splitChunks(str, 1) | |
}); | |
suite.add("2 length substring", function () { | |
// 2 length substring | |
splitChunks(str, 2) | |
}); | |
suite.add("3 length substring", function () { | |
// 3 length substring | |
splitChunks(str, 3) | |
}); | |
suite.add("4 length substring", function () { | |
// 4 length substring | |
splitChunks(str, 4) | |
}); | |
suite.add("5 length substring", function () { | |
// 5 length substring | |
splitChunks(str, 5) | |
}); | |
suite.add("6 length substring", function () { | |
// 6 length substring | |
splitChunks(str, 6) | |
}); | |
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 various lengths"); | |
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