pax_global_header00006660000000000000000000000064141053314740014514gustar00rootroot0000000000000052 comment=2e57f8c95e5e1f40379c6f85d18aee962662eb22 yocto-queue-1.0.0/000077500000000000000000000000001410533147400137715ustar00rootroot00000000000000yocto-queue-1.0.0/.editorconfig000066400000000000000000000002571410533147400164520ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 yocto-queue-1.0.0/.gitattributes000066400000000000000000000000231410533147400166570ustar00rootroot00000000000000* text=auto eol=lf yocto-queue-1.0.0/.github/000077500000000000000000000000001410533147400153315ustar00rootroot00000000000000yocto-queue-1.0.0/.github/workflows/000077500000000000000000000000001410533147400173665ustar00rootroot00000000000000yocto-queue-1.0.0/.github/workflows/main.yml000066400000000000000000000006261410533147400210410ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 16 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test yocto-queue-1.0.0/.gitignore000066400000000000000000000000271410533147400157600ustar00rootroot00000000000000node_modules yarn.lock yocto-queue-1.0.0/.npmrc000066400000000000000000000000231410533147400151040ustar00rootroot00000000000000package-lock=false yocto-queue-1.0.0/index.d.ts000066400000000000000000000021441410533147400156730ustar00rootroot00000000000000export default class Queue implements Iterable { /** The size of the queue. */ readonly size: number; /** Tiny queue data structure. The instance is an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols), which means you can iterate over the queue front to back with a β€œfor…of” loop, or use spreading to convert the queue to an array. Don't do this unless you really need to though, since it's slow. @example ``` import Queue from 'yocto-queue'; const queue = new Queue(); queue.enqueue('πŸ¦„'); queue.enqueue('🌈'); console.log(queue.size); //=> 2 console.log(...queue); //=> 'πŸ¦„ 🌈' console.log(queue.dequeue()); //=> 'πŸ¦„' console.log(queue.dequeue()); //=> '🌈' ``` */ constructor(); [Symbol.iterator](): IterableIterator; /** Add a value to the queue. */ enqueue(value: ValueType): void; /** Remove the next value in the queue. @returns The removed value or `undefined` if the queue is empty. */ dequeue(): ValueType | undefined; /** Clear the queue. */ clear(): void; } yocto-queue-1.0.0/index.js000066400000000000000000000023751410533147400154450ustar00rootroot00000000000000/* How it works: `this.#head` is an instance of `Node` which keeps track of its current value and nests another instance of `Node` that keeps the value that comes after it. When a value is provided to `.enqueue()`, the code needs to iterate through `this.#head`, going deeper and deeper to find the last value. However, iterating through every single item is slow. This problem is solved by saving a reference to the last value as `this.#tail` so that it can reference it to add a new value. */ class Node { value; next; constructor(value) { this.value = value; } } export default class Queue { #head; #tail; #size; constructor() { this.clear(); } enqueue(value) { const node = new Node(value); if (this.#head) { this.#tail.next = node; this.#tail = node; } else { this.#head = node; this.#tail = node; } this.#size++; } dequeue() { const current = this.#head; if (!current) { return; } this.#head = this.#head.next; this.#size--; return current.value; } clear() { this.#head = undefined; this.#tail = undefined; this.#size = 0; } get size() { return this.#size; } * [Symbol.iterator]() { let current = this.#head; while (current) { yield current.value; current = current.next; } } } yocto-queue-1.0.0/index.test-d.ts000066400000000000000000000002551410533147400166510ustar00rootroot00000000000000import {expectType} from 'tsd'; import Queue from './index.js'; const queue = new Queue(); queue.enqueue('πŸ¦„'); expectType(queue.dequeue()); yocto-queue-1.0.0/license000066400000000000000000000021351410533147400153370ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. yocto-queue-1.0.0/package.json000066400000000000000000000014401410533147400162560ustar00rootroot00000000000000{ "name": "yocto-queue", "version": "1.0.0", "description": "Tiny queue data structure", "license": "MIT", "repository": "sindresorhus/yocto-queue", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "type": "module", "exports": "./index.js", "engines": { "node": ">=12.20" }, "scripts": { "//test": "xo && ava && tsd", "test": "ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "queue", "data", "structure", "algorithm", "queues", "queuing", "list", "array", "linkedlist", "fifo", "enqueue", "dequeue", "data-structure" ], "devDependencies": { "ava": "^3.15.0", "tsd": "^0.17.0", "xo": "^0.44.0" } } yocto-queue-1.0.0/readme.md000066400000000000000000000040531410533147400155520ustar00rootroot00000000000000# yocto-queue [![](https://badgen.net/bundlephobia/minzip/yocto-queue)](https://bundlephobia.com/result?p=yocto-queue) > Tiny queue data structure You should use this package instead of an array if you do a lot of `Array#push()` and `Array#shift()` on large arrays, since `Array#shift()` has [linear time complexity](https://medium.com/@ariel.salem1989/an-easy-to-use-guide-to-big-o-time-complexity-5dcf4be8a444#:~:text=O(N)%E2%80%94Linear%20Time) *O(n)* while `Queue#dequeue()` has [constant time complexity](https://medium.com/@ariel.salem1989/an-easy-to-use-guide-to-big-o-time-complexity-5dcf4be8a444#:~:text=O(1)%20%E2%80%94%20Constant%20Time) *O(1)*. That makes a huge difference for large arrays. > A [queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) is an ordered list of elements where an element is inserted at the end of the queue and is removed from the front of the queue. A queue works based on the first-in, first-out ([FIFO](https://en.wikipedia.org/wiki/FIFO_(computing_and_electronics))) principle. ## Install ``` $ npm install yocto-queue ``` ## Usage ```js import Queue from 'yocto-queue'; const queue = new Queue(); queue.enqueue('πŸ¦„'); queue.enqueue('🌈'); console.log(queue.size); //=> 2 console.log(...queue); //=> 'πŸ¦„ 🌈' console.log(queue.dequeue()); //=> 'πŸ¦„' console.log(queue.dequeue()); //=> '🌈' ``` ## API ### `queue = new Queue()` The instance is an [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols), which means you can iterate over the queue front to back with a β€œfor…of” loop, or use spreading to convert the queue to an array. Don't do this unless you really need to though, since it's slow. #### `.enqueue(value)` Add a value to the queue. #### `.dequeue()` Remove the next value in the queue. Returns the removed value or `undefined` if the queue is empty. #### `.clear()` Clear the queue. #### `.size` The size of the queue. ## Related - [quick-lru](https://github.com/sindresorhus/quick-lru) - Simple β€œLeast Recently Used” (LRU) cache yocto-queue-1.0.0/test.js000066400000000000000000000022671410533147400153150ustar00rootroot00000000000000import test from 'ava'; import Queue from './index.js'; test('.enqueue()', t => { const queue = new Queue(); queue.enqueue('πŸ¦„'); t.is(queue.dequeue(), 'πŸ¦„'); queue.enqueue('🌈'); queue.enqueue('❀️'); t.is(queue.dequeue(), '🌈'); t.is(queue.dequeue(), '❀️'); }); test('.dequeue()', t => { const queue = new Queue(); t.is(queue.dequeue(), undefined); t.is(queue.dequeue(), undefined); queue.enqueue('πŸ¦„'); t.is(queue.dequeue(), 'πŸ¦„'); t.is(queue.dequeue(), undefined); }); test('.clear()', t => { const queue = new Queue(); queue.clear(); queue.enqueue(1); queue.clear(); t.is(queue.size, 0); queue.enqueue(1); queue.enqueue(2); queue.enqueue(3); queue.clear(); t.is(queue.size, 0); }); test('.size', t => { const queue = new Queue(); t.is(queue.size, 0); queue.clear(); t.is(queue.size, 0); queue.enqueue('πŸ¦„'); t.is(queue.size, 1); queue.enqueue('πŸ¦„'); t.is(queue.size, 2); queue.dequeue(); t.is(queue.size, 1); queue.dequeue(); t.is(queue.size, 0); queue.dequeue(); t.is(queue.size, 0); }); test('iterable', t => { const queue = new Queue(); queue.enqueue('πŸ¦„'); queue.enqueue('🌈'); t.deepEqual([...queue], ['πŸ¦„', '🌈']); });