JavaScript snippets.
Dependency-free functions for the problems you hit every week. Copy the code, read the gotchas, run it in the browser first.
Safely parse a string to an integer
A toInt() helper that returns a fallback instead of NaN, rejects '12px' and '1e3', and never guesses the radix.
Convert dates across time zones (DST-safe)
Format any instant in a named IANA time zone, and get the current UTC offset for that zone — daylight saving handled by the browser.
Format a date without a library
YYYY-MM-DD and ISO-style output, plus a 'time ago' helper, using only built-in APIs.
Debounce and throttle
Limit how often a function runs: debounce waits for a pause in calls, throttle guarantees at most one call per interval.
Deep clone an object
structuredClone vs the JSON trick vs a recursive clone — what each one silently drops.
Group, unique and chunk an array
The three lodash helpers people install a whole library for — groupBy, uniqBy and chunk — in a few lines each.
Slugify a string for URLs
Turn 'Crème Brûlée & Friends!' into 'creme-brulee-and-friends' — strips accents, handles symbols, trims dashes.
Format bytes and numbers for humans
'1.2 MB', '3,456,789' and '$1,234.50' using Intl.NumberFormat — correct for every locale.
Retry fetch with exponential backoff
Retry failed requests with growing delays and jitter, only for errors that are worth retrying.
Sleep, timeout and cancel with AbortController
An awaitable sleep, a withTimeout() wrapper for any promise, and cancellation that actually stops the underlying fetch.
Parse and build query strings
Read ?page=2&tag=a&tag=b into an object, and build a URL from an object — with arrays, without hand-rolled encoding.
localStorage with expiry and JSON
A tiny wrapper that stores objects, expires them after a TTL, and doesn't crash in private browsing or when the quota is full.
Escape a string for use in a regex
Turn user input like 'price (USD)' into a pattern that matches it literally, so a stray ( or . doesn't break or change your search.
Run promises with a concurrency limit
Process 500 URLs with at most 5 requests in flight — a pLimit / async pool in a dozen lines, keeping results in order.
Parse CSV in the browser
A correct CSV parser in 25 lines: quoted fields, embedded commas and newlines, doubled quotes — plus a File input example.
Format a duration from milliseconds
90061000 → '1d 1h 1m 1s', plus a clock-style '01:30:05' and a rounded '2 hours' — without a date library.
Get and set nested values by path
get(obj, 'a.b[0].c') and set(obj, 'a.b.c', 1) that never throw on missing keys and create intermediate objects on write.
Deep equality check
Compare two values structurally — nested objects, arrays, Dates, Maps, Sets, NaN — the way you wish === worked.
Validate an email address or URL
Practical validation that accepts real addresses and rejects obvious typos — and why you should stop looking for the perfect email regex.
Memoize a function with an LRU cache
Cache expensive results by argument, cap the cache size, evict least-recently-used entries, and support async functions.