String split by length (v12)

Revision 12 of this benchmark created on


Preparation HTML

<script>

function splitRegex(str, len) {
  var regex = new RegExp('.{1,' + len + '}', 'g');
  return str.match(regex);
}

function splitSlice(str, len) {
  var _size = Math.ceil(str.length/len),
      ret  = new Array(_size)  
  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

Ready to run.

Testing in
TestOps/sec
Regex w/ Short String
splitRegex(shortString, 2);
ready
Slice w/ Short String
splitSlice(shortString, 2);
ready
Slice2 w/ Short String
splitSlice2(shortString, 2);
ready
Regex w/ Long String
splitRegex(longString, 2);
ready
Slice w/ Long String
splitSlice(longString, 2);
ready
Slice2 w/ Long String
splitSlice2(longString, 2);
ready
Regex w/ Long String, Large Chunks
splitRegex(longString, 100);
ready
Slice w/ Long String, Large Chunks
splitSlice(longString, 100);
ready
Slice2 w/ Large String, Large Chunks
splitSlice2(longString, 100);
ready

Revisions

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