A lightweight LIFO stack implemented on top of a dynamic array with chunked growth/shrink.
Designed for speed and memory efficiency versus a Collection-backed stack, while preserving a clean API.
- Fast push/pop using a dynamic array with headspace (ChunkSize)
- Lower memory use than Collection(≈2× less per item on x64; extra headspace applies)
- Shrink logic: reclaims capacity when headspace grows to 2 * ChunkSize
- Enumeration: For Each(top → bottom) via an intermediateCollection
- Utility export: Items([base])returns a 0- or 1-based array copy
- Pure VBA, no external references
| Member | Type | Description | 
|---|---|---|
| Push(Item) | Sub | Adds an item at the top. | 
| Pop() | Function | Returns and removes the top item. Raises error 5 if empty. | 
| Peek(Default) | Property | Returns the top item without removing it. Raises error 5 if empty. | 
| Count | Property | Number of items currently stored. | 
| IsEmpty | Property | Trueif empty, elseFalse. | 
| Clear | Sub | Clears the stack and resets capacity to one ChunkSize. | 
| Items([base]) | Function | Copies items into Variant(); order is top → bottom. | 
| For Each | Enumerator | Iterates top → bottom. (Don’t mutate while iterating.) | 
Errors
- Empty stack on Peek/PopraisesvbErrorInvalidProcedureCall (=5)with source"Stack.Peek"/"Stack.Pop".
Dim s As New Stack
s.Push "alpha"
s.Push "beta"
Debug.Print s.Peek        ' -> beta
Debug.Print s.Pop         ' -> beta (removed)
Debug.Print s.Pop         ' -> alpha (removed)
Debug.Print s.IsEmpty     ' -> True
The array-based implementation provides constant-time O(1) Push and Pop operations for normal workloads,
while outperforming the Collection-based version in speed and memory use.
| Count | Array | Collection | 
|---|---|---|
| 10 | 0.00025 | 0.00049 | 
| 100 | 0.00025 | 0.00049 | 
| 1 000 | 0.00025 | 0.00050 | 
| 10 000 | 0.00025 | 0.00049 | 
| 100 000 | 0.00281 | 0.00050 | 
- The array version is roughly 2× faster than the Collectionversion at small to medium stack sizes.
- It uses about half the memory per stored item on x64 systems (excluding the headspace buffer).
- When the stack grows beyond several hundred thousand items, occasional ReDim Preserveoperations cause timing spikes.
 These are amortized and remain negligible for practical workloads.
- The collection version stays stable at very large sizes but pays a constant late-binding penalty internally.
- Resizing follows a chunked strategy (ChunkSize = 100) to balance allocation overhead and memory fragmentation.
- For computational stacks, recursion simulations, or evaluation engines where typical depth ≤ 100 000,
 prefer the array stack for speed and memory efficiency.
- For unbounded or long-lived stacks (e.g., message buffers), the collection stack may offer smoother scalability.
- Increasing ChunkSizereduces the frequency ofReDim Preservecalls at the cost of higher idle headspace.
All timings were measured in VBA 7 (x64) on Windows 11 using a high-resolution
Stopwatchbased onQueryPerformanceCounter.