String split by length

JavaScript performance comparison

Revision 8 of this test case created by supsup

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
}

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

var splitRegexCached = (function(){
  var regexCache = {};
  return( function(str, len) {
    var regex = regexCache[len] || (regexCache[len] = new RegExp('[\\s\\S]{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;
}

function splitSubstr(str, len) {
  var ret = [ ];
  for (var offset = 0, strLen = str.length; offset < strLen; offset += len) {
    ret.push(str.substr(offset, len));
  }
  return ret;
}

var simpleWhileSubstr = function (string, length) {
    var array = [];
   
    while (string.length > 0) {
        array.push(string.slice(0, length));
        string = string.substr(length);
    }
   
    return array;
};

var simpleSubstrRec = function(targetString, targetAmount){

    function stringSplitter(stringToChunk,numberOfChars)
    {    
               
        if(stringToChunk.length > numberOfChars){        
            result[result.length] = stringToChunk.substr(0, numberOfChars);
            stringSplitter(stringToChunk.substr(numberOfChars, stringToChunk.length), numberOfChars);
            return result;            
        }else{
              result[result.length] = stringToChunk;        
        }
       
       
    }

    var result = [];
    result.concat(stringSplitter(targetString, targetAmount));
    return result;
}









var tinyString  = (new Array(39)).join('abcdefghijklmnopqrstuvwxyz');
var shortString  = (new Array(385)).join('abcdefghijklmnopqrstuvwxyz');
var mediumString  = (new Array(3856)).join('abcdefghijklmnopqrstuvwxyz');
var longString  = (new Array(38462)).join('abcdefghijklmnopqrstuvwxyz');
var spanArray = [6666, 2, 3339, 5, 1620, 13, 847, 22, 521, 46,  254, 88, 145];
var spanLength = spanArray.length;
var i = 0, span;
</script>
<script>
Benchmark.prototype.teardown = function() {
    i = 0;
};
</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
fasterSplit

fasterSplit(tinyString, 15);
fasterSplit(shortString, 15);
fasterSplit(mediumString, 15);
fasterSplit(longString, 15);
pending…
Regex (Cached)

splitRegexCached(tinyString, 15);
splitRegexCached(shortString, 15);
splitRegexCached(mediumString, 15);
splitRegexCached(longString, 15);
 
pending…
string.Substr

splitSubstr(tinyString, 15);
splitSubstr(shortString, 15);
splitSubstr(mediumString, 15);
splitSubstr(longString, 15);
pending…
string.Slice

splitSlice(tinyString, 15);
splitSlice(shortString, 15);
splitSlice(mediumString, 15);
splitSlice(longString, 15);
pending…
Simple Substr recursion

simpleSubstrRec(tinyString, 15);
simpleSubstrRec(shortString, 15);
simpleSubstrRec(mediumString, 15);
simpleSubstrRec(longString, 15);
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