# Simple Queue Implementation in JavaScript

**URL:** <https://forum.kirupa.com/t/simple-queue-implementation-in-javascript/637077>\
**Category:** web dev\
**Created:** [August 21, 2017, 8:21pm UTC](https://forum.kirupa.com/t/simple-queue-implementation-in-javascript/637077 "2017-08-21T20:21:57Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [August 22, 2017, 8:28pm UTC](https://forum.kirupa.com/t/simple-queue-implementation-in-javascript/637077/3 "2017-08-22T20:28:25Z")

</div>

Just for kicks, I created a version that doesn’t use `shift`:

```
class Queue {
    constructor(...items) {
        this.items = items;
        this.start = 0;
    }
    clear() {
        this.items.length = 0;
    }
    clone() {
        var newItems = this.items.slice(this.start);

        return new Queue(...newItems);
    }
    contains(item) {
        return this.items.includes(item);
    }
    peek() {
        var item = null;

        if (this.items.length > this.start) {
            item = this.items[this.start];
        }
        
        return item;
    }
    dequeue() {
        var removedItem = this.items[this.start];
        this.start++;

        return removedItem;
    }
    enqueue(item) {
        this.items.push(item);
        return item;
    }
}

```

Usage of this should be the same as the earlier. In my cursory testing, As I mention below, it is slower than the `shift` approach for some bizarre reason.

Cheers,  
Kirupa

---

_[View the full topic](https://forum.kirupa.com/t/simple-queue-implementation-in-javascript/637077)._
