Published on

Implementing Queues with NodeJS and Javascript

Authors
  • avatar
    Name
    Jonathan Juliani
    Twitter

Queues with NodeJS/JavaScript

Dealing with Sequential Data

Queues are one of the main data structures and one of the most used by a lot of softwares to deal with sequential processes and stuff like that. Queues follow the FIFO principle, which means, first in, first out. In other words the first element to get to the queue will be the first to be removed or processed.

implementing-queues-with-nodejs-and-javascript

Implementation

Queues like I said above have to be an structure that adds new elements as the last element and removes/processes starting from the first position (index).

Just like Stacks, queues can be implemented using arrays with NodeJS/Javascript. A basic implementation should have functions (or methods) such as: enqueue, dequeue, peak or get to check the first element and so on...e.g:

class Queue {
  constructor() {
    this.items = []
  }

  // Adds an element to the queue
  enqueue(element) {
    this.items.push(element)
  }

  // Removes the first element of the queue
  dequeue() {
    if (this.isEmpty()) return 'Underflow' // Fila vazia
    return this.items.shift()
  }

  // Get the first element of the queue (without removing it)
  front() {
    if (this.isEmpty()) return 'No elements in Queue'
    return this.items[0]
  }

  // Check if the queue is empty
  isEmpty() {
    return this.items.length === 0
  }

  // Logs the queue
  printQueue() {
    let str = ''
    for (let i = 0; i < this.items.length; i++) str += this.items[i] + ' '
    console.log(str)
  }
}

Where we Use Queues

Managing Tasks

When we think about operating systems they use queues a lot. Each process needs to be executed in order, otherwise we would have a lot of problems while using our computers. Queues are responsable to guarantee the right execution of the processes that arrive.

Managing Events

Web servers, web pages, mobile apps, we have tons of applications that respond to events, and those events need to be handled and processed in a sequential and ordered way, queues usually are useful in those situations as well.

Tips

  • I don't need to say it again, but I will: keep the queue implementation as simple as possible. There are a bunch of libs that have stable queues implementations if you need, but in case you want to implement, keep it simple and with only the essential functions
  • Consider using Linked-Lists (you can see more about Linked-Lists here) to have a more consise and easy to maintain queue.
  • If you do implement a queue and you will use it in your application consider implementing error verifications for enqueue and dequeue operations

In Summary

Queues help us to deal with events and tasks that need to be executed in a specific order, eventually you will be using or maintaining a system that uses queues.

Have you ever created queues to your NodeJS/JavaScript projects? Share your experiences, challenges or insights in the comments below. Let's learn together!