
Product
Microsoft Teams Notifications Are Now Available in Socket
Socket can now send alerts and supply chain attack notifications to Microsoft Teams, with filters that route the right updates to each channel.
@structkit/queue
Advanced tools
A native-like Queue implementation with full TypeScript support and Array-like iterators
A native-like Queue implementation for TypeScript that behaves like built-in JavaScript data structures. Array-based implementation provides fast performance with complete type safety.
global.Queue, usable like Array or Setfor...of, spread operator, destructuring, and moreQueue<T>, ReadonlyQueue<T>)entries, keys, values)util.inspect customizationSymbol.dispose for automatic iterator cleanuppnpm install @structkit/queue
import '@structkit/queue' // import this on your project's entry file
// Create with new keyword
const queue = new Queue<number>()
// Create with initial values (array)
const queue2 = new Queue([1, 2, 3, 4, 5])
// Create from any iterable
const queue3 = new Queue(new Set([1, 2, 3]))
const queue = new Queue<number>()
// Add elements (enqueue)
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
// Remove and return first element (dequeue)
const first = queue.dequeue() // 1
// Peek at first element without removing
const peek = queue.peek() // 2
// Check queue size
console.log(queue.size) // 2
// Check if queue is empty
console.log(queue.isEmpty()) // false
// Check if value exists
console.log(queue.has(2)) // true
// Clear the queue
queue.clear()
console.log(queue.isEmpty()) // true
const queue = new Queue([1, 2, 3, 4, 5])
// for...of loop
for (const value of queue) {
console.log(value) // 1, 2, 3, 4, 5
}
// Spread operator
const array = [...queue] // [1, 2, 3, 4, 5]
// Destructuring
const [first, second, ...rest] = queue
console.log(first) // 1
console.log(second) // 2
console.log(rest) // [3, 4, 5]
// Array.from
const newArray = Array.from(queue)
// Convert to Set (remove duplicates)
const uniqueSet = new Set(queue)
const queue = new Queue(['a', 'b', 'c'])
// entries(): Returns [index, value] pairs
for (const [index, value] of queue.entries()) {
console.log(index, value)
}
// 0 'a'
// 1 'b'
// 2 'c'
// keys(): Returns indices
for (const index of queue.keys()) {
console.log(index)
}
// 0, 1, 2
// values(): Returns values
for (const value of queue.values()) {
console.log(value)
}
// 'a', 'b', 'c'
const queue = new Queue([1, 2, 3])
// Basic usage
queue.forEach((value) => {
console.log(value)
})
// Value is passed twice (similar to Set)
queue.forEach((value1, value2, queue) => {
console.log(value1 === value2) // true
})
// thisArg support
const context = { multiplier: 2 }
queue.forEach(function(value) {
console.log(value * this.multiplier)
}, context)
// 2, 4, 6
function bfs(graph: Map<string, string[]>, start: string) {
const queue = new Queue([start])
const visited = new Set<string>()
while (!queue.isEmpty()) {
const node = queue.dequeue()!
if (visited.has(node)) continue
visited.add(node)
console.log(node)
const neighbors = graph.get(node) || []
neighbors.forEach(neighbor => queue.enqueue(neighbor))
}
}
interface Task {
id: string
execute: () => Promise<void>
}
class TaskQueue {
private queue = new Queue<Task>()
private processing = false
add(task: Task) {
this.queue.enqueue(task)
this.process()
}
private async process() {
if (this.processing || this.queue.isEmpty()) return
this.processing = true
while (!this.queue.isEmpty()) {
const task = this.queue.dequeue()!
await task.execute()
}
this.processing = false
}
get pending() {
return this.queue.size
}
}
function slidingWindowMax(nums: number[], k: number): number[] {
const result: number[] = []
const queue = new Queue<number>()
for (let i = 0; i < nums.length; i++) {
queue.enqueue(nums[i])
if (queue.size === k) {
result.push(Math.max(...queue))
queue.dequeue()
}
}
return result
}
// With new keyword
new Queue<T>(values?: readonly T[] | Iterable<T> | null): Queue<T>
// Without new keyword (function call style)
Queue<T>(): Queue<T>
Queue<T>(...items: T[]): Queue<T>
// From iterable
new Queue<T>(iterable?: Iterable<T> | null): Queue<T>
Parameters:
values - Optional array or iterable to initialize the queue with...items - Variable number of items to add to the queue (when called without new)Examples:
const q1 = new Queue([1, 2, 3]) // from array
const q2 = new Queue(new Set([1, 2, 3])) // from Set
const q3 = new Queue('abc') // from string
const q4 = Queue(1, 2, 3) // variadic
const q5 = Queue<number>() // empty queue
enqueue(value: T): voidAdds an element to the end of the queue.
dequeue(): T | undefinedRemoves and returns the first element from the queue. Returns undefined if the queue is empty.
peek(): T | undefinedReturns the first element from the queue without removing it.
isEmpty(): booleanReturns true if the queue is empty.
clear(): voidRemoves all elements from the queue.
has(value: T): booleanChecks whether the queue contains the specified value.
forEach(callbackfn, thisArg?): voidExecutes a callback function for each element in the queue.
[Symbol.iterator](): QueueIterator<T>Makes the queue iterable.
entries(): QueueIterator<[number, T]>Returns an iterator of [index, value] pairs.
keys(): QueueIterator<number>Returns an iterator of indices.
values(): QueueIterator<T>Returns an iterator of values.
size: number (readonly)Returns the number of elements in the queue.
[Symbol.toStringTag]: string (readonly)Returns "Queue" for Object.prototype.toString.call().
Features:
next(), return(), throw())[Symbol.iterator]() returns itself)Symbol.dispose (TC39 Stage 3)util.inspectUsage with using (Explicit Resource Management):
// Automatic cleanup with 'using' declaration
{
using iterator = queue.values()
for (const value of iterator) {
if (value > 10) break // Iterator automatically cleaned up
}
} // Symbol.dispose called here
pnpm build
# Run tests
pnpm test
# Run tests in watch mode
pnpm test:watch
pnpm typecheck
enqueue: O(1) amortizeddequeue: O(n) (uses Array.shift)peek: O(1)has: O(n)clear: O(1)💡 Note: Currently uses Array-based implementation. Future versions may switch to a linked list to improve
dequeueperformance to O(1).
MIT
Issues and PRs are welcome!
FAQs
A native-like Queue implementation with full TypeScript support and Array-like iterators
We found that @structkit/queue demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.

Product
Socket can now send alerts and supply chain attack notifications to Microsoft Teams, with filters that route the right updates to each channel.

Security News
pnpm 12 rewrites the package manager in Rust, cutting install times by up to 90% while preserving pnpm 11 workflows and lockfiles.

Security News
Socket CTO Ahmad Nassri joins AppSec leaders at Black Hat to discuss active malware, package manager risks, and software supply chain defense.