2d456h789ot5

JavaScript performance comparison

Test case created by artalar

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  let bit = 0;
  let factor = 1;
  function createId() {
      const parts = new Array(factor);
      let _factor = 0;
      while (_factor < factor)
          parts[_factor++] = 0;
      if (++bit < 30)
          parts[factor - 1] = (parts[factor - 1] || 1) << bit;
      else {
          factor++;
          bit = 0;
          parts.push(0);
      }
      return parts;
  }
  class MaskSet {
      constructor(mask) {
          this.parts = mask ? mask.parts.slice() : [];
      }
      add(id) {
          const _parts = this.parts;
          for (let i = 0; i < id.length; i++) {
              if (i === _parts.length)
                  _parts.push(id[i]);
              else
                  _parts[i] = _parts[i] | id[i];
          }
      }
      has(id) {
          const _parts = this.parts;
          if (!Array.isArray(id) || id.length > _parts.length)
              return false;
          for (let i = 0; i < id.length; i++)
              if (_parts[i] !== (_parts[i] | id[i]))
                  return false;
          return true;
      }
  }
  
  
  /////////////////////////////////
  const length = 500
  const mask = new MaskSet()
  const maskIds = {}
  const set = new Set()
  const setIds = {}
  const arr = []
  const arrIds = {}
  
  let i = 0
  while(i++ < length * 2) {
    const maskId = maskIds[i] = createId()
    const setId = setIds[i] = i
    const arrId = arrIds[i] = i
    if (i % 2) {
      mask.add(maskId)
      set.add(setId)
      arr.push(arrId)
    }
  }
  
  const getRand = () => (length * 2 * Math.random()) | 0
  
  let isInclude

};
</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
mask
isInclude = mask.has(maskIds[getRand()])
pending…
set
isInclude = set.has(setIds[getRand()])
pending…
arr
isInclude = arr.includes(arrIds[getRand()])
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