d23r4t5y6u7i8

JavaScript performance comparison

Test case created by artalar

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const createQueue = () => {
    const queue = new Map();
    let min = 0;
    let max = 0;
    return {
      insert(priority, el) {
        let part = queue.get(priority);
        if (!part) queue.set(priority, (part = []));
        if (!part.includes(el)) {
          part.push(el);
          min = Math.min(min, priority); // useful only for cycles
          max = Math.max(max, priority);
        }
      },
      extract() {
        let part = queue.get(min);
        while (!part || !part.length) {
          if (min === max) return;
          part = queue.get(++min);
        }
        return part.shift();
      }
    };
  };
  
  class Queue {
    constructor() {
      this.min = 0;
      this.max = 0;
      this.queue = new Map();
    }
    insert(priority, el) {
      let part = this.queue.get(priority);
      if (!part) this.queue.set(priority, (part = []));
      if (!part.includes(el)) {
        part.push(el);
        this.min = Math.min(this.min, priority); // useful only for cycles
        this.max = Math.max(this.max, priority);
      }
    }
    extract() {
      let part = this.queue.get(this.min);
      while (!part || !part.length) {
        if (this.min === this.max) return;
        part = this.queue.get(++this.min);
      }
      return part.shift();
    }
  }
  
  const createEl = () => ({ value: Math.random() });
  
  const qObj = createQueue();
  const qCls = new Queue();
  const el1 = createEl();
  const el2 = createEl();
  const inserts = [
    [0, createEl()],
    [2, createEl()],
    [2, createEl()],
    [2, createEl()],
    [3, createEl()],
    [4, el2],
    [4, el2],
    [4, createEl()],
    [5, createEl()],
    [5, 2],
    [5, 2],
    [6, createEl()],
    [11, createEl()],
  ];

};
</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
createQueue()
createQueue()
pending…
new Queue()
new Queue()
pending…
qObj
inserts.forEach(a => qObj.insert(...a));
let c;
while ((c = qObj.extract())) 0;
pending…
qCls
inserts.forEach(a => qCls.insert(...a));
let c;
while ((c = qCls.extract())) 0;
pending…

Revisions

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

0 Comments