String split by length (v5)

Revision 5 of this benchmark created on


Preparation HTML

<script>

 
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

Ready to run.

Testing in
TestOps/sec
Regex w/ Short String
fasterSplit(shortString, 2);
ready
Regex w/ Long String
fasterSplit(longString, 2);
ready
Slice w/ Short String
splitSlice(shortString, 2);
ready
Slice w/ Long String
splitSlice(longString, 2);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.