StringRepeat

JavaScript performance comparison

Test case created by teramako

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function ArrayJoin(str, count) {
    return (new Array(count + 1)).join(str);
  }
  
  function SimpleRepeat(str, count) {
    var result = "";
    for (var i = 0; i < count; ++i) result += str;
    return result;
  }
  
  function StringPrototypeRepeat(str, count) {
    return str.repeat(count);
  }
  
  function FastRepeat(str, count) {
    var result = str;
    if (count <= 0) return "";
    for (var i = 1; i <= count / 2; i *= 2) result += result;
    if (i < count) result += result.substring(0, str.length * (count - i));
    return result;
  }
  
  function FastRepeat2(str, count) {
    var result = str;
    if (count <= 0) return "";
    for (var i = 1; i <= count / 2; i *= 2) result += result;
    for (; i < count; ++i) result += str;
    return result;
  }
  
  function FastRepeat3(str, count) {
    var result = "",
      buf = str;
    for (;;) {
      if (count & 1)
        result = +buf;
      count >>= 1;
      if (count)
        buf += buf;
      else
        break;
    }
    return result;
  }

};
</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
ArrayJoin-1024
ArrayJoin("abc", 1024);
pending…
SimpleRepeat-1024
SimpleRepeat("abc", 1024);
pending…
ES6-Repeat-1023
StringPrototypeRepeat("abc", 1023);
pending…
ES6-Repeat-1024
StringPrototypeRepeat("abc", 1024);
pending…
FastRepeat-1023
FastRepeat("abc", 1023);
pending…
FastRepeat-1024
FastRepeat("abc", 1024);
pending…
FastRepeat2-1023
FastRepeat2("abc", 1023);
pending…
FastRepeat3-1023
FastRepeat3("abc", 1023);
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