erase string with spaces

JavaScript performance comparison

Test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function repeat(pattern, count) {
      if (count < 1) return '';
      var result = '';
      while (count > 0) {
          if (count & 1) result += pattern;
          count >>= 1, pattern += pattern;
      }
      return result;
  }
  
  var str = "12345678901234567890123456789012345678901234567890";
  str += str+str; str += str+str; str += str+str; //str += str+str; 
  
  var spaces = new Array(256+1).join(' ');
  
  var growBuf = "                ";

};
</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
repeat
var a = repeat(' ', str.length);
pending…
regex
var b = str.replace(/./g, ' ');
pending…
join
var c = new Array(str.length+1).join(' ');
pending…
substr
var d = str.length > 256 ? 
    str.replace(/./g, ' ') : 
    spaces.substr(0, str.length); 
pending…
substr2
var e = str.length > 256 ? 
    new Array(Math.floor(str.length / 256)+1).join(spaces)+ 
        spaces.substr(0, str.length % 256) : 
    spaces.substr(0, str.length); 
pending…
substr3
var f =  str.length > 256 ? 
    spaces.substr(0, str.length >> 8).replace(/./g, spaces)+
        spaces.substr(0, str.length & 255) :
    spaces.substr(0, str.length); 
pending…
growing buffer
while(growBuf.length < str.length) growBuf += growBuf;

var g = growBuf.substr(0, str.length); 
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.

0 Comments