ts-data-forge
    Preparing search index...

    Function createStack

    • Creates a new Stack instance with LIFO (Last-In, First-Out) behavior using a high-performance dynamic array.

      This factory function creates an optimized stack implementation that maintains excellent performance characteristics for both push and pop operations. The underlying dynamic array automatically resizes to accommodate growing workloads while providing predictable O(1) operations.

      Implementation Features:

      • O(1) push operations (amortized - occasionally O(n) when resizing)
      • O(1) pop operations (always)
      • Automatic buffer resizing - starts at 8 elements, doubles when full
      • Memory efficient - garbage collects removed elements immediately
      • Dynamic array design - eliminates need for complex memory management

      Performance Benefits:

      • No array shifting required for stack 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 stack.

      Parameters

      • OptionalinitialValues: readonly T[]

        Optional array of initial elements to populate the stack. Elements will be popped in reverse order of how they appear in the array (last array element will be popped first). If provided, the initial buffer capacity will be at least twice the array length.

      Returns Stack<T>

      A new Stack instance optimized for high-performance LIFO operations.

      const stack = createStack<string>();

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

      stack.push('first');
      // eslint-disable-next-line unicorn/prefer-single-call
      stack.push('second');

      assert.notOk(stack.isEmpty);
      assert(stack.size === 2);
      assert.deepStrictEqual(stack.pop(), Optional.some('second'));
      assert.deepStrictEqual(stack.pop(), Optional.some('first'));
      assert.deepStrictEqual(stack.pop(), Optional.none);

      const seededStack = createStack([10, 20, 30]);

      assert(seededStack.size === 3);
      assert.deepStrictEqual(seededStack.pop(), Optional.some(30));