Code Library

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

11 results

Optimize Images
BASH

Optimize Images

Turn a folder of photos into WebP width variants with ImageMagick. Smaller sources are not upscaled, and existing files are skipped.

View code
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
Token Bucket
PHP

Token Bucket

Allow a short burst, then refill tokens at a steady rate. The burst cannot exceed the bucket size.

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
Connection Pool
PHP

Connection Pool

Reuse an idle connection inside the same process, instead of opening a new one for every query.

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
Password Hashing
PHP

Password Hashing

Store a password as a salted hash, with a pepper from app config. This is not reversible encryption.

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.