Code Library

Practical snippets you can use freely, each with a short description and a real-world example.

7 results

Binary Search Tree
JAVASCRIPT

Binary Search Tree

Insert, search, and delete while keeping values sorted. Average O(log n), worst O(n) if the tree becomes a chain.

View code
Function Memoization
JAVASCRIPT

Function Memoization

Wrap a function and reuse its result for the same inputs. The next call with the same values skips the work.

View code
LRU Cache
JAVASCRIPT

LRU Cache

Keep a fixed number of entries and evict the least recently used item when the cache is full.

View code
Breadth and Depth Traversal
JAVASCRIPT

Breadth and Depth Traversal

An undirected graph: visit in breadth or depth, find the shortest hop path, and detect a cycle without counting the walk back to a parent.

View code
Async Queue
JAVASCRIPT

Async Queue

Run a limited number of jobs at once, retry after a failure, and start the highest priority first.

View code
Observer Pattern
JAVASCRIPT

Observer Pattern

Subscribe a listener to an event and call it on emit by priority. once runs a single time.

View code
KMP Search
JAVASCRIPT

KMP Search

Find a pattern in text in linear time, using a table of the longest prefix that is also a suffix.

View code

Need a snippet that is not here?

Describe the problem or idea, and I will see if it belongs in the library.