Code Library

Code Snippets

Working, copy-ready patterns you'll reach for again and again.

JavaScriptJun 2026

Fetch with retry on failure

Retries a failed fetch request up to N times with exponential backoff between attempts.

async function fetchWithRetry(url, options = {}, retries = 3) {
  for (let attempt = 0; attempt < retries; att…
FetchRetryError Handling
View Snippet →
VueJun 2026

useLocalStorage composable

A reactive ref that syncs with localStorage — value persists across page refreshes.

import { ref, watch } from 'vue'

export function useLocalStorage(key, defaultValue) {
  const stored = localS…
ComposablelocalStorageReactivity
View Snippet →
JavaScriptMay 2026

Debounce function

Delays a function call until a given wait period has passed — ideal for search inputs and resize events.

function debounce(fn, wait = 300) {
  let timer
  return function (...args) {
    clearTimeout(timer)
    time…
PerformanceEventsUtility
View Snippet →
JavaScriptMay 2026

Deep clone without JSON.parse

Clones an object deeply without losing Dates, undefined values, or circular references.

// Modern: structuredClone (Node 17+, all modern browsers)
const clone = structuredClone(original)
// Handles:…
ObjectsUtilitystructuredClone
View Snippet →
PythonApr 2026

Chunked file reading

Read large files in fixed-size chunks to avoid loading the entire file into memory.

def read_in_chunks(filepath, chunk_size=1024 * 64):
    """Yield 64 KB chunks from a binary file."""
    with…
File I/OMemoryGenerator
View Snippet →
TypeScriptApr 2026

Generic runtime type guard

Narrow an unknown value to a typed interface at runtime — useful after fetch or JSON.parse.

function isType<T>(value: unknown, keys: (keyof T)[]): value is T {
  return (
    typeof value === 'object' &…
Type GuardsGenericsRuntime Safety
View Snippet →