stringsplitting

JavaScript performance comparison

Test case created by cyberxwolf

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function chunk(str,size){
        var count = Math.ceil(str.length/size);
        var d = new Array(count);
        return doChunk(0,count,d,str,size);
        }
        
        function doChunk(curIndex,endIndex,curArr,curStr,size){
        curArr[curIndex] = curStr.substr(0,size);
        if(curIndex < endIndex-1){
        curIndex++;
        curStr = curStr.substr(size);
        return doChunk(curIndex,endIndex,curArr,curStr,size);
        }else{
        return curArr;
        }
         
        }
        function chunk2(str,size){
        var count = Math.ceil(str.length/size);
        var d = new Array(count);
        return doChunk2(0,count,d,str,size);
        }
        function doChunk2(curIndex,endIndex,curArr,curStr,size){
        curArr[curIndex] = curStr.substr(curIndex*size,size);
        if(curIndex < endIndex-1){
        curIndex++;
        return doChunk2(curIndex,endIndex,curArr,curStr,size);
        }else{
        return curArr;
        }
         
        }
      function ch2(v,l){
      var n = Math.ceil(v.length/l),r = new Array(n);
      var g=0;
      for(var i = 0, o = 0; i < n; ++i, o = g) {
      g+=l;
      r[i] = v.substr(o,l);
      }
      return r;
      }
      function ch3(v,l){
      var n = Math.ceil(v.length/l),r = new Array(n),i=0;
      while(i<n) {++i;
      r[i] = v.substr(i*l,l);
      }
      return r;
      }
      function ch(v,l){var r = new Array(Math.ceil(v.length/l)),n = r.length,i=0,o=0,g=0;while(i<n){o=g;r[i] = v.substr(o,l);i++;g+=l;}return r;}
        var chunkStr = {
        chunk:  function(v,l){var i = 0,n = Math.ceil(v.length/l),r = new Array(n);while(i < n){r[i] = v.substr(i*l,l);i++;} return r;}
        };
       var fastChunk =   function(v,l){var i = 0,n = Math.ceil(v.length/l),r = [];while(i < n){r[i] = v.substr(i*l,l);i++;} return r;}
         function fastChunk2(v,l){var i = 0,n = Math.ceil(v.length/l),r = [];while(i < n){r[i] = v.substr(i*l,l);i++;} return r;}
      
       var fastChunk3 =   function(v,l){var i = 0,r = new Array(Math.ceil(v.length/l)),n = r.length;while(i < n){r[i] = v.substr(i*l,l);i++;} return r;}
      
       var fastChunk3x = function(v,l){var i = 0,r = new Array(),n = 0;
      while(i < v.length){
      r[n] = v.substr(i,l);i+=l;
      n++;
      }
      return r;}
       var fastChunk3xa = function(v,l){var i = 0,r = new Array(Math.ceil(v.length/l)),n = 0;
      while(i < v.length){
      r.push(v.substr(i,l));i+=l;
      }
      return r;}
      var fastChunk3xb = function(v,l){var i = 0,r = new Array(),n = 0;
      for(;i < v.length;i+=l){
      r.push(v.substr(i,l));
      }
      return r;}
      var fastChunk3xc = function(v,l){var i = 0,r = new Array(Math.ceil(v.length/l)),n = 0;
      for(;i < v.length;i+=l){
      r[n] = v.substr(i,l);
      n++;
      }
      return r;}
      
      function fastChunk4(v,l){var i = 0,r = new Array(Math.ceil(v.length/l)),n = r.length;while(i < n){r[i] = v.substr(i*l,l);i++;}return r;}
      var fastChunk5 =   function(v,l){var i = 0,n = parseInt(v.length/l)+1,r = [];while(i < n){r[i] = v.substr(i*l,l);i++;} return r;}
         function fastChunk6(v,l){var i = 0,n = parseInt(v.length/l)+1,r = [];while(i < n){r[i] = v.substr(i*l,l);i++;} return r;}
       var fastChunk7 =   function(v,l){var i = 0,r = new Array(parseInt(v.length/l)+1),n = r.length;while(i < n){r[i] = v.substr(i*l,l);i++;} return r;}
        function fastChunk8(v,l){var i = 0,r = new Array(parseInt(v.length/l)+1),n = r.length;while(i < n){r[i] = v.substr(i*l,l);i++;} return r;}
      
          function chunk3(str,size){
        var count = Math.ceil(str.length/size);
        var d = new Array(count);var i = 0;
        while(i<count){
         d[i] = str.substr(i*size,size);
      i++;
        }
        return d;
        }
      
       function chunk3c(str,size){
        var count = Math.ceil(str.length/size);
        var d = [];var i = 0;
      var lp = setInterval(()=>{  
      if(i<count){
         d[i] = str.substr(i*size,size);
      i++;
        }else{
      d[i] = str.substr(i*size,size);
      clearInterval(lp);
      lp = false;
      }
      },1);
      var lop = 0;
      while(d.length < count){
      
      }
        return d;
        }
       function chunk3b(str,size){
        var count = Math.ceil(str.length/size);
        var d = new Array(count);
      var i = 0;
        while(i<count){
      setTimeout(()=>{
         d[i] = str.substr(i*size,size);
      i++;
      },1);
        }
        return d;
        }
        function chunk4(str,size){
        var count = Math.ceil(str.length/size);
        var d = new Array(count);
      
        for(var i = 0,o = 0;i<count;i++){
         d[i] = str.substr(o,size);o+=size;
        }
      return d;
        }
       function chunk5(str,size){
        var count = Math.ceil(str.length/size);
        var d = new Array(count);
      var i = 0;var o = 0;
        while(i<count){
         d[i] = str.substr(o,size);
      i++;o+=size;
        }
      return d;
        }
                  function splitString(str,size){
        return str.split(eval("/(.{"+size+"})/g")).filter(O=>O);
        }
            function chunkSubstr1(str, size) {
            var chunks = new Array(str.length / size + .5 | 0),
                nChunks = chunks.length;
          
            var newo = 0;
            for(var i = 0, o = 0; i < nChunks; ++i, o = newo) {
              newo += size;
              chunks[i] = str.substr(o, size);
            }
          
            return chunks;
          }
          
          function chunkSubstr2(str, size) {
            var numChunks = str.length / size + .5 | 0,
                chunks = new Array(numChunks);
          
            for(var i = 0, o = 0; i < numChunks; ++i, o += size) {
              chunks[i] = str.substr(o, size);
            }
          
            return chunks;
          }
        
        var chunkFast = new function(){
          this.str = "";
          this.size = 0;
          this.curIndex = 0;
          this.endIndex = 0,
          this.result = [];
        this.start = function(val,len){
        this.str = val;
        this.curIndex = 0;
        this.size = len;
        this.endIndex = Math.ceil(val.length/len);
        this.result = new Array(this.endIndex);
        return this.chunk();
        };
        this.chunk = function(){
            this.result[this.curIndex] = this.str.substr(this.curIndex*this.size,this.size);
            if(this.curIndex < this.endIndex-1){
            this.curIndex++;
            return this.chunk();
            }else{
            return this.result;
            }
             
            }
          };
      function getChunks(str, size) {
        const strLength = str.length;
        const numChunks = Math.ceil(strLength / size);
        const chunks = new Array(numChunks);
      
        let i = 0;
        let o = 0;
      
        for (; i < numChunks; ++i, o += size) {
          chunks[i] = str.substr(o, size);
        }
      
        return chunks;
      }
        var chunkFastPreSetup = new function(){
          this.str = "";
          this.size = 0;
          this.curIndex = 0;
          this.endIndex = 0,
          this.result = [];
        this.start = function(val,len){
        this.str = val;
        this.size = len;
        this.curIndex = 0;
        this.endIndex = Math.ceil(val.length/len);
        this.result = new Array(this.endIndex);
        };
        this.chunk = function(){
            this.result[this.curIndex] = this.str.substr(this.curIndex*this.size,this.size);
            if(this.curIndex < this.endIndex-1){
            this.curIndex++;
            return this.chunk();
            }else{
            return this.result;
            }
             
            }
          };
        
        var fastChunk9 =   function(v,l){
      var i = 0,n=0,r = [];//new Array(Math.ceil(v.length/l)),n = r.length;
      while(i < v.length){
      r[n] = v.substr(i,l);
      i+=l;n++;
      }
       return r;
      }
        var fastChunk10 =   function(v,l){
      var i = 0,n=0,r = new Array(Math.ceil(v.length/l));//,n = r.length;
      while(i < v.length){
      r[n] = v.substr(i,l);
      i+=l;
      n++;
      }
       return r;
      }
      var fastChunk11 =   function(v,l){
      var i = 0,r = [];
      while(i*l < v.length){
      r[i] = v.substr(i*l,l);
      i++;
      }
      return r;
      }
        var fastChunk12 =   function(v,l){
      var i = 0,r = new Array(Math.ceil(v.length/l));
      while(i*l < v.length){
      r[i] = v.substr(i*l,l);
      i++;
      }
       return r;
      }
        
         var fastChunk3a =   function(v,l){
      var i = 0,r = new Array(Math.ceil(v.length/l)),n = r.length;
      for(;i<n;++i){r[i] = v.substr(i*l,l);}
       return r;}
      
          function fastChunk4a(v,l){var i = 0,r = new Array(Math.ceil(v.length/l)),n = r.length;
      for(;i<n;++i){r[i] = v.substr(i*l,l);}
      return r;}
      
         var fastChunk7a =   function(v,l){var i = 0,r = new Array(parseInt(v.length/l)+1),n = r.length;
      for(;i<n;++i){r[i] = v.substr(i*l,l);}
       return r;}
      
      var fastChunk12a =   function(v,l){
      var i = 0,r = new Array(Math.ceil(v.length/l));
      for(;i*l<v.length;++i){r[i] = v.substr(i*l,l);}
       return r;
      }
      
        var testStr = "01234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345678901234567891234567890123456789123456789012345678912345";
        
      var startAt = 24;
      var endAt = 32;//8192
      
      var milliseconds = 1;
    var seconds = milliseconds * 1000;
    var minutes = seconds * 60;
    var hours = minutes * 60;
    var days = hours * 24;
    var years = days * 365;
      var start = 0,end = 0,to = 0;
  

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
fc4tst4
startAt = 128;
fastChunk4(testStr,startAt);
pending…
chunkSubstr1
chunkSubstr1(testStr,startAt);
pending…
fc4tst3
startAt = 256;
fastChunk4(testStr,startAt);
pending…
fc4tst2
startAt = 512;
fastChunk4(testStr,startAt);
pending…
fc4tst1
startAt = 1024;
fastChunk4(testStr,startAt);
pending…

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

Compare results of other browsers

0 Comments