stackoverflow question 55823298

JavaScript performance comparison

Test case created by salmanarshad2000

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function check_regexp(str) {
      return /^(.+)\1+$/.test(str)
  }
  function check_recursive(str) {
      function isRepeating(p, str) {
          if (str.length > p.length) {
              var left = str.substring(0, p.length);
              var right = str.substring(p.length, str.length);
              return left === p && isRepeating(p, right);
          }
          return str === p;
      }
      if (str.length == 1) {
          return true;
      }
      for (var i = 1; i <= str.length / 2; i++) {
          if (str.length % i != 0) {
              continue;
          }
          var p = str.substring(0, i);
          if (isRepeating(p, str)) {
              return true;
          }
      }
      return false;
  }
  function check_zfn(str) {
      var n = str.length;
      var z = Array(n).fill(0);
      var i, l, r;
      for (i = 1, l = 0, r = 0; i < n; ++i) {
          if (i <= r) z[i] = Math.min(r - i + 1, z[i - l]);
          while (i + z[i] < n && str[z[i]] == str[i + z[i]])
              ++z[i];
          if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
      }
      return z.some((zi, i) => (i + zi) === n && n % i === 0);
  }
  function check_zfn_v2(str) {
      var n = str.length,
          lps = Array(n),
          len = 0,
          i = 1;
      lps[0] = 0;
      while (i < n) {
          if (str.charAt(i) === str.charAt(len)) {
              lps[i++] = ++len;
          } else {
              if (len !== 0) {
                  len = lps[len - 1];
              } else {
                  lps[i++] = 0;
              }
          }
      }
      len = lps[n - 1];
      return len > 0 && n % (n - len) === 0;
  }
  function* primeFactors(n) {
      for (var k = 2; k * k <= n; k++) {
          if (n % k == 0) {
              yield k
              do {
                  n /= k
              } while (n % k == 0)
          }
      }
      if (n > 1) yield n
  }
  function check_prime(str) {
      var n = str.length
      primeloop: for (var p of primeFactors(n)) {
          var l = n / p
          var s = str.substring(0, l)
          for (var j = 1; j < p; j++) {
              if (s != str.substring(l * j, l * (j + 1))) continue primeloop
          }
          return true
      }
      return false
  }
  function check_rotation(str) {
      return (str + str).indexOf(str, 1) !== str.length;
  }
  /*
   * tests
   */
  function randomString(length) {
      var lookup = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      var result = "";
      for (var i = 0; i < length; i++) {
          result += lookup.charAt(Math.floor(Math.random() * lookup.length));
      }
      return result;
  }
  var results = [];
  var tests = Array.from({ length: 100 }, function () {
      var length = Math.floor(Math.random() * 99) + 2;
      var repeat = Math.floor(Math.random() * 100) + 1;
      var result = repeat === 1 ? false : Math.random() >= .5;
      var string = randomString(length).repeat(repeat);
      if (result === false) {
          var index = Math.floor(Math.random() * string.length);
          string = string.substr(0, index) + "%" + string.substr(index + 1);
      }
      results.push(result);
      return string;
  });
  [check_regexp, check_recursive, check_zfn, check_zfn_v2, check_prime, check_rotation].forEach(function (fn) {
      var i = tests.findIndex(function (test, i) {
          return fn(test) !== results[i];
      });
      if (i >= 0) {
          console.log(fn.name + " failed; expected " + results[i] + " for " + tests[i]);
      }
  });
  

};
</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
check_regexp
var i, j = tests.length, r;
for (i = 0; i < j; i++) r = check_regexp(tests[i]);
pending…
check_recursive
var i, j = tests.length, r;
for (i = 0; i < j; i++) r = check_recursive(tests[i]);
pending…
check_zfn
var i, j = tests.length, r;
for (i = 0; i < j; i++) r = check_zfn(tests[i]);
pending…
check_zfn_v2
var i, j = tests.length, r;
for (i = 0; i < j; i++) r = check_zfn_v2(tests[i]);
pending…
check_prime
var i, j = tests.length, r;
for (i = 0; i < j; i++) r = check_prime(tests[i]);
pending…
check_rotation
var i, j = tests.length, r;
for (i = 0; i < j; i++) r = check_rotation(tests[i]);
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