Editing Comparison of queue implementations This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) Comparison of a few ways to implement a buffer (FIFO queue). http://safalra.com/web-design/javascript/queues/Queue.js Vs Array push/shift Vs two arrays Vs linked list Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) <script src="http://safalra.com/web-design/javascript/queues/Queue.js"> </script> Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) var workingSize = 10; var iterations = 100000; Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code var i; var a = []; for (i = 0; i <= workingSize; i++) { a.push(i); } for (i = 0; i <= iterations; i++) { a.shift(); a.push(i); } Test 2 Title Async (check if this is an asynchronous test) Code var i; var q = new Queue(); for (i = 0; i < workingSize; i++) { q.enqueue(i); } for (i = 0; i < iterations; i++) { q.dequeue(); q.enqueue(i); } Test 3 Title Async (check if this is an asynchronous test) Code var i; var inbox = []; var outbox = []; function enq(i) { inbox.push(i); } function deq() { if (outbox.length === 0) { while (inbox.length !== 0) { outbox.push(inbox.pop()); } } return outbox.pop(); } for (i = 0; i < workingSize; i++) { enq(i); } for (i = 0; i < iterations; i++) { deq(); enq(i); } Test 4 Title Async (check if this is an asynchronous test) Code var head; var tail; function enq(i) { tail = tail ? tail.next = {value: i} : head = {value: i}; } function deq() { var value = head ? head.value : undefined; head = head == tail ? (tail = undefined) : head.next; return undefined; } for (i = 0; i < workingSize; i++) { enq(i); } for (i = 0; i < iterations; i++) { deq(); enq(i); } Test 5 Title Async (check if this is an asynchronous test) Code var i; var inbox = []; var outbox = []; function enq(i) { inbox.push(i); } function deq() { if (outbox.length === 0) { var t = outbox; outbox = inbox.reverse(); inbox = t; } return outbox.pop(); } for (i = 0; i < workingSize; i++) { enq(i); } for (i = 0; i < iterations; i++) { deq(); enq(i); } Test 6 Title Async (check if this is an asynchronous test) Code var i; var inbox = []; var outbox = []; function enq(i) { inbox.push(i); } function deq() { if (outbox.length === 0) { outbox = inbox.reverse(); inbox = []; } return outbox.pop(); } for (i = 0; i < workingSize; i++) { enq(i); } for (i = 0; i < iterations; i++) { deq(); enq(i); } Test 7 Title Async (check if this is an asynchronous test) Code function fastQueue(){ this.tail = []; this.head = Array.prototype.slice.call(arguments); this.offset = 0; }; fastQueue.prototype.shift = function shift() { if (this.offset === this.head.length) { var tmp = this.head; tmp.length = 0; this.head = this.tail; this.tail = tmp; this.offset = 0; if (this.head.length === 0) return; } return this.head[this.offset++]; }; fastQueue.prototype.push = function push(item) { var ret = this.tail.push(item); var self = this; return ret; }; var i; var q = new fastQueue(); for (i = 0; i < workingSize; i++) { q.push(i); } for (i = 0; i < iterations; i++) { q.shift(); q.push(i); } Test 8 Title Async (check if this is an asynchronous test) Code var list = []; var idx = 0; var MIN = 100; function enq(e) { list.push(e); } function deq() { var val = list[idx]; var len = list.length / 2; if (idx > len && idx > MIN) { idx -= len; list.splice(0, len); } return val; } for (i = 0; i < workingSize; i++) { enq(i); } for (i = 0; i < iterations; i++) { deq(); enq(i); }