String split by length

JavaScript performance comparison

Revision 5 of this test case created and last updated

Preparation code

<script type="text/javascript">

 
function fasterSplit(str,len){
  var ret=[],strlen=str.length,off=0
  do {
    ret.push(str.substr(off,len))
    off+=len
  } while (off<strlen)
  return ret
}

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;
}

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.

Testing in unknown unknown
Test Ops/sec
Regex w/ Short String
fasterSplit(shortString, 2);
pending…
Regex w/ Long String
fasterSplit(longString, 2);
pending…
Slice w/ Short String
splitSlice(shortString, 2);
pending…
Slice w/ Long String
splitSlice(longString, 2);
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:

0 comments

Add a comment