String split by length
JavaScript performance comparison
Info
Add SplitSlice2 method; spoiler, it's better.
Preparation code
<script type="text/javascript">
function splitRegex(str, len) {
var regex = new RegExp('.{1,' + len + '}', 'g');
return str.match(regex);
}
function splitSlice(str, len) {
var ret = [ ];
for (var offset = 0, strLen = str.length; offset < strLen; offset += len) {
ret.push(str.slice(offset, len + offset));
}
return ret;
}
// splitSlice2 ftw
// https://github.com/naomik
function splitSlice2(str, len) {
var _size = Math.ceil(str.length/len),
_ret = new Array(_size)
;
for (var _i=0; _i<_size; _i++) {
_ret[_i] = str.substring(_i*len, len);
}
return _ret;
}
var shortString = 'abcdefg';
var longString = (new Array(500)).join('qwertyuiopasdfghjklzxcvbnm');
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Regex w/ Short String |
|
pending… |
Slice w/ Short String |
|
pending… |
Slice2 w/ Short String |
|
pending… |
Regex w/ Long String |
|
pending… |
Slice w/ Long String |
|
pending… |
Slice2 w/ Long String |
|
pending… |
Regex w/ Long String, Large Chunks |
|
pending… |
Slice w/ Long String, Large Chunks |
|
pending… |
Slice2 w/ Large String, Large Chunks |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 1: published by James Brumond
- Revision 2: published by Jon-Carlos Rivera
- Revision 3: published by gildas
- Revision 5: published and last updated
- Revision 6: published
- Revision 7: published
- Revision 8: published by supsup
- Revision 9: published by Tgr
- Revision 10: published by Naomi Kyoto
0 comments