ts-data-forge
    Preparing search index...

    Function createQueue

    • Creates a new Queue instance with FIFO (First-In, First-Out) behavior using a high-performance circular buffer.

      This factory function creates an optimized queue implementation that maintains excellent performance characteristics for both enqueue and dequeue operations. The underlying circular buffer automatically resizes to accommodate growing workloads while providing predictable O(1) operations.

      Implementation Features:

      • O(1) enqueue operations (amortized - occasionally O(n) when resizing)
      • O(1) dequeue operations (always)
      • Automatic buffer resizing - starts at 8 elements, doubles when full
      • Memory efficient - garbage collects removed elements immediately
      • Circular buffer design - eliminates need for array shifting operations

      Performance Benefits:

      • No array copying during normal operations
      • Minimal memory allocation overhead
      • Predictable performance under high load
      • Efficient memory usage with automatic cleanup

      Type Parameters

      • T

        The type of elements stored in the queue.

      Parameters

      • OptionalinitialValues: readonly T[]

        Optional array of initial elements to populate the queue. Elements will be dequeued in the same order they appear in the array. If provided, the initial buffer capacity will be at least twice the array length.

      Returns Queue<T>

      A new Queue instance optimized for high-performance FIFO operations.

      const queue = createQueue<number>();

      assert.ok(queue.isEmpty);
      assert(queue.size === 0);

      queue.enqueue(1);
      queue.enqueue(2);

      assert.notOk(queue.isEmpty);
      assert(queue.size === 2);
      assert.deepStrictEqual(queue.dequeue(), Optional.some(1));
      assert.deepStrictEqual(queue.dequeue(), Optional.some(2));
      assert.deepStrictEqual(queue.dequeue(), Optional.none);

      const seededQueue = createQueue(['first', 'second']);

      assert(seededQueue.size === 2);
      assert.deepStrictEqual(seededQueue.dequeue(), Optional.some('first'));
      assert.deepStrictEqual(seededQueue.dequeue(), Optional.some('second'));