<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: R (RYUSLOG DEV)</title>
    <description>The latest articles on DEV Community by R (RYUSLOG DEV) (@rshday).</description>
    <link>https://dev.to/rshday</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4247385%2F8d12e67b-b0ce-4599-bad3-df9cd3278df5.png</url>
      <title>DEV Community: R (RYUSLOG DEV)</title>
      <link>https://dev.to/rshday</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rshday"/>
    <language>en</language>
    <item>
      <title>React useEffect: Dependency Arrays, Cleanup Functions, and Race Conditions</title>
      <dc:creator>R (RYUSLOG DEV)</dc:creator>
      <pubDate>Mon, 16 Nov 2026 09:59:12 +0000</pubDate>
      <link>https://dev.to/rshday/react-useeffect-dependency-arrays-cleanup-functions-and-race-conditions-jga</link>
      <guid>https://dev.to/rshday/react-useeffect-dependency-arrays-cleanup-functions-and-race-conditions-jga</guid>
      <description>&lt;h2&gt;
  
  
  Quick Summary
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;useEffect&lt;/code&gt; synchronizes a component with something outside React: a subscription, the DOM, a network request, a timer. It is not a lifecycle hook and it is not the place for derived state. The effect function runs after the browser paints, and the optional cleanup runs before the next execution of the same effect and once on unmount.&lt;/p&gt;

&lt;p&gt;The dependency array is compared item by item with &lt;code&gt;Object.is&lt;/code&gt;. &lt;code&gt;[]&lt;/code&gt; means "never re-run", &lt;code&gt;[a, b]&lt;/code&gt; means "re-run when either changes", and omitting the array means "run after every render". Every reactive value used inside the effect belongs in the array — that is what the &lt;code&gt;react-hooks/exhaustive-deps&lt;/code&gt; lint enforces, and disabling it is how stale closures become production bugs.&lt;/p&gt;

&lt;p&gt;Race conditions are the second half of the story. A fetch started on one render can resolve after a newer fetch has already come back, overwriting fresh data with stale data. React's answer is cleanup: return a function that aborts the request or ignores its result, and React guarantees it runs before the next effect.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Syntax &amp;amp; Signature
&lt;/h2&gt;

&lt;p&gt;tsx&lt;br&gt;
import { useEffect, useState } from "react";&lt;/p&gt;

&lt;p&gt;export function Status({ userId }: { userId: string }) {&lt;br&gt;
  const [status, setStatus] = useState("idle");&lt;/p&gt;

&lt;p&gt;// Runs once on mount; cleanup runs on unmount.&lt;br&gt;
  useEffect(() =&amp;gt; {&lt;br&gt;
    const id = setInterval(() =&amp;gt; setStatus("tick"), 1000);&lt;br&gt;
    return () =&amp;gt; clearInterval(id);&lt;br&gt;
  }, []);&lt;/p&gt;

&lt;p&gt;// Runs on mount and whenever userId changes.&lt;br&gt;
  useEffect(() =&amp;gt; {&lt;br&gt;
    if (!userId) return;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const controller = new AbortController();

async function run() {
  try {
    const res = await fetch(`/api/status/${userId}`, {
      signal: controller.signal,
    });
    if (!res.ok) throw new Error(`HTTP ${res.status}`);
    const data = (await res.json()) as { state: string };
    setStatus(data.state);
  } catch (err) {
    if (err instanceof DOMException &amp;amp;&amp;amp; err.name === "AbortError") return;
    setStatus("error");
  }
}

run();
return () =&amp;gt; controller.abort();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}, [userId]);&lt;/p&gt;

&lt;p&gt;return &lt;/p&gt;
&lt;p&gt;{status}&lt;/p&gt;;&lt;br&gt;
}&lt;br&gt;
The type signatures, which explain why the callback cannot be &lt;code&gt;async&lt;/code&gt;:

&lt;p&gt;ts&lt;br&gt;
function useEffect(&lt;br&gt;
  setup: () =&amp;gt; void | (() =&amp;gt; void),&lt;br&gt;
  deps?: readonly unknown[]&lt;br&gt;
): void;&lt;/p&gt;

&lt;p&gt;// Not valid: an async function returns a Promise, not a cleanup function.&lt;br&gt;
// useEffect(async () =&amp;gt; { ... }, []);&lt;br&gt;
&lt;code&gt;useLayoutEffect&lt;/code&gt; has the same signature and runs synchronously before the browser paints — use it only for measurements that must happen before the user sees the frame.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Comparison Table
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Dependency array&lt;/th&gt;
&lt;th&gt;When the effect runs&lt;/th&gt;
&lt;th&gt;Typical case&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;omitted&lt;/td&gt;
&lt;td&gt;After every render&lt;/td&gt;
&lt;td&gt;Rare; subscriptions that must resync constantly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;[]&lt;/td&gt;
&lt;td&gt;Once after mount&lt;/td&gt;
&lt;td&gt;Global listeners, timers with no reactive input&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;[a]&lt;/td&gt;
&lt;td&gt;After mount and whenever a changes&lt;/td&gt;
&lt;td&gt;Fetch on id change, subscribe to an external store&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;[a, b]&lt;/td&gt;
&lt;td&gt;Whenever either changes&lt;/td&gt;
&lt;td&gt;Multi-input synchronization&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;[a?.id, b]&lt;/td&gt;
&lt;td&gt;Primitive deps avoid object identity churn&lt;/td&gt;
&lt;td&gt;Prefer primitives over objects&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;[obj] with a new object each render&lt;/td&gt;
&lt;td&gt;Every render&lt;/td&gt;
&lt;td&gt;Bug: memoize or destructure&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Hook&lt;/th&gt;
&lt;th&gt;Timing&lt;/th&gt;
&lt;th&gt;Use for&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;useEffect&lt;/td&gt;
&lt;td&gt;After paint, async&lt;/td&gt;
&lt;td&gt;Data fetching, subscriptions, logging&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;useLayoutEffect&lt;/td&gt;
&lt;td&gt;Before paint, synchronous&lt;/td&gt;
&lt;td&gt;DOM measurement, scroll restoration&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;useInsertionEffect&lt;/td&gt;
&lt;td&gt;Before layout, for CSS-in-JS&lt;/td&gt;
&lt;td&gt;Injecting styles&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Event handler&lt;/td&gt;
&lt;td&gt;On interaction&lt;/td&gt;
&lt;td&gt;Anything triggered by a user action&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Render body&lt;/td&gt;
&lt;td&gt;During render&lt;/td&gt;
&lt;td&gt;Pure computation only — no side effects&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Race condition fix&lt;/th&gt;
&lt;th&gt;Mechanism&lt;/th&gt;
&lt;th&gt;Fits&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;AbortController&lt;/td&gt;
&lt;td&gt;Cancels the request itself&lt;/td&gt;
&lt;td&gt;fetch, streams&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ignore flag&lt;/td&gt;
&lt;td&gt;Discards the result in cleanup&lt;/td&gt;
&lt;td&gt;Libraries without cancellation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;useSyncExternalStore&lt;/td&gt;
&lt;td&gt;React-managed subscription&lt;/td&gt;
&lt;td&gt;External stores&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Request id / sequence check&lt;/td&gt;
&lt;td&gt;Compare latest id before setting&lt;/td&gt;
&lt;td&gt;Rapid successive queries&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Practical Edge Cases &amp;amp; Deep Dives
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. The fetch race, and why cleanup is the fix
&lt;/h3&gt;

&lt;p&gt;tsx&lt;br&gt;
function Profile({ id }: { id: string }) {&lt;br&gt;
  const [user, setUser] = useState&amp;lt;{ name: string } | null&amp;gt;(null);&lt;/p&gt;

&lt;p&gt;// Broken: the slow first request can overwrite the fast second one.&lt;br&gt;
  useEffect(() =&amp;gt; {&lt;br&gt;
    fetch(&lt;code&gt;/api/users/${id}&lt;/code&gt;)&lt;br&gt;
      .then((r) =&amp;gt; r.json())&lt;br&gt;
      .then((data) =&amp;gt; setUser(data));&lt;br&gt;
  }, [id]);&lt;/p&gt;

&lt;p&gt;// Correct: the cleanup discards the stale response.&lt;br&gt;
  useEffect(() =&amp;gt; {&lt;br&gt;
    let ignore = false;&lt;br&gt;
    fetch(&lt;code&gt;/api/users/${id}&lt;/code&gt;)&lt;br&gt;
      .then((r) =&amp;gt; r.json())&lt;br&gt;
      .then((data) =&amp;gt; {&lt;br&gt;
        if (!ignore) setUser(data);&lt;br&gt;
      });&lt;br&gt;
    return () =&amp;gt; {&lt;br&gt;
      ignore = true;&lt;br&gt;
    };&lt;br&gt;
  }, [id]);&lt;/p&gt;

&lt;p&gt;return &lt;/p&gt;
&lt;p&gt;{user ? user.name : "…"}&lt;/p&gt;;&lt;br&gt;
}&lt;br&gt;
Switching &lt;code&gt;id&lt;/code&gt; from &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;2&lt;/code&gt; mounts the second effect before the response for &lt;code&gt;1&lt;/code&gt; arrives. Without the flag, the older response applies to the newer id and the page shows the wrong user until something else triggers a render. The &lt;code&gt;ignore&lt;/code&gt; closure works because each effect invocation captures its own &lt;code&gt;ignore&lt;/code&gt; variable.

&lt;h3&gt;
  
  
  2. Infinite loops from unstable dependencies
&lt;/h3&gt;

&lt;p&gt;tsx&lt;br&gt;
function Search({ query }: { query: string }) {&lt;br&gt;
  const [results, setResults] = useState([]);&lt;/p&gt;

&lt;p&gt;// Infinite: &lt;code&gt;options&lt;/code&gt; is a new object every render.&lt;br&gt;
  const options = { limit: 10 };&lt;br&gt;
  useEffect(() =&amp;gt; {&lt;br&gt;
    fetchResults(query, options).then(setResults);&lt;br&gt;
  }, [query, options]);&lt;/p&gt;

&lt;p&gt;// Fixed: depend on primitives, or memoize the object.&lt;br&gt;
  useEffect(() =&amp;gt; {&lt;br&gt;
    fetchResults(query, { limit: 10 }).then(setResults);&lt;br&gt;
  }, [query]);&lt;/p&gt;

&lt;p&gt;return &lt;/p&gt;
&lt;ul&gt;{results.map((r) =&amp;gt; &lt;li&gt;{r}&lt;/li&gt;)}&lt;/ul&gt;;&lt;br&gt;
}

&lt;p&gt;function fetchResults(q: string, _opts: { limit: number }): Promise {&lt;br&gt;
  return Promise.resolve(q ? [&lt;code&gt;${q}-1&lt;/code&gt;] : []);&lt;br&gt;
}&lt;br&gt;
Objects, arrays and inline functions are recreated on every render, so &lt;code&gt;Object.is&lt;/code&gt; never matches. Depend on the primitive pieces (&lt;code&gt;limit&lt;/code&gt;, &lt;code&gt;query&lt;/code&gt;) instead. When the effect truly needs a function from props, wrap it in &lt;code&gt;useCallback&lt;/code&gt; in the parent — or move the function &lt;em&gt;inside&lt;/em&gt; the effect if nothing outside it calls the function.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. &lt;code&gt;setInterval&lt;/code&gt; and the stale closure
&lt;/h3&gt;

&lt;p&gt;tsx&lt;br&gt;
function Ticker() {&lt;br&gt;
  const [count, setCount] = useState(0);&lt;/p&gt;

&lt;p&gt;// Wrong: the interval captures count === 0 forever.&lt;br&gt;
  // useEffect(() =&amp;gt; {&lt;br&gt;
  //   const id = setInterval(() =&amp;gt; setCount(count + 1), 1000);&lt;br&gt;
  //   return () =&amp;gt; clearInterval(id);&lt;br&gt;
  // }, []);&lt;/p&gt;

&lt;p&gt;// Right: the updater form reads the latest queued value.&lt;br&gt;
  useEffect(() =&amp;gt; {&lt;br&gt;
    const id = setInterval(() =&amp;gt; setCount((c) =&amp;gt; c + 1), 1000);&lt;br&gt;
    return () =&amp;gt; clearInterval(id);&lt;br&gt;
  }, []);&lt;/p&gt;

&lt;p&gt;return &lt;/p&gt;
&lt;p&gt;{count}&lt;/p&gt;;&lt;br&gt;
}&lt;br&gt;
Adding &lt;code&gt;count&lt;/code&gt; to the dependency array "fixes" this and destroys it at the same time: the interval is cleared and recreated every second. The functional updater is the correct answer whenever the new state depends on the old one and the effect must stay mounted.

&lt;h3&gt;
  
  
  4. StrictMode double invocation in development
&lt;/h3&gt;

&lt;p&gt;tsx&lt;br&gt;
function Chat({ roomId }: { roomId: string }) {&lt;br&gt;
  useEffect(() =&amp;gt; {&lt;br&gt;
    const connection = createConnection(roomId);&lt;br&gt;
    connection.connect();&lt;br&gt;
    // This cleanup runs immediately after the first mount in StrictMode dev,&lt;br&gt;
    // then the effect runs again. Idempotent cleanup is mandatory.&lt;br&gt;
    return () =&amp;gt; connection.disconnect();&lt;br&gt;
  }, [roomId]);&lt;/p&gt;

&lt;p&gt;return &lt;/p&gt;
&lt;p&gt;connected to {roomId}&lt;/p&gt;;&lt;br&gt;
}

&lt;p&gt;function createConnection(roomId: string) {&lt;br&gt;
  return {&lt;br&gt;
    connect: () =&amp;gt; console.log("connect", roomId),&lt;br&gt;
    disconnect: () =&amp;gt; console.log("disconnect", roomId),&lt;br&gt;
  };&lt;br&gt;
}&lt;br&gt;
React 18 StrictMode mounts, unmounts, and remounts every component once in development to surface exactly this class of bug: an effect that connects but never disconnects leaks a socket in production after every navigation. Production builds do not double-invoke, so a leak found this way is real.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Effects are for external systems, not for derived state
&lt;/h3&gt;

&lt;p&gt;tsx&lt;br&gt;
type Item = { id: number; price: number; qty: number };&lt;/p&gt;

&lt;p&gt;function Cart({ items }: { items: Item[] }) {&lt;br&gt;
  const [total, setTotal] = useState(0);&lt;/p&gt;

&lt;p&gt;// Wrong: an extra render, and a flash of the previous total.&lt;br&gt;
  useEffect(() =&amp;gt; {&lt;br&gt;
    setTotal(items.reduce((sum, i) =&amp;gt; sum + i.price * i.qty, 0));&lt;br&gt;
  }, [items]);&lt;/p&gt;

&lt;p&gt;// Right: compute during render; no state, no effect, no flash.&lt;br&gt;
  const computed = items.reduce((sum, i) =&amp;gt; sum + i.price * i.qty, 0);&lt;/p&gt;

&lt;p&gt;// Memoized only when the computation is expensive.&lt;br&gt;
  // const computed = useMemo(() =&amp;gt; expensive(items), [items]);&lt;/p&gt;

&lt;p&gt;return &lt;/p&gt;
&lt;p&gt;{total} / {computed}&lt;/p&gt;;&lt;br&gt;
}&lt;br&gt;
Anything derivable from props or state during render should be computed during render. The effect version renders twice for every input change, and worse, between the two renders the UI shows a value that cannot exist.

&lt;h3&gt;
  
  
  6. Subscribing to an external store correctly
&lt;/h3&gt;

&lt;p&gt;tsx&lt;br&gt;
import { useSyncExternalStore } from "react";&lt;/p&gt;

&lt;p&gt;function subscribe(callback: () =&amp;gt; void) {&lt;br&gt;
  const onScroll = () =&amp;gt; callback();&lt;br&gt;
  window.addEventListener("scroll", onScroll);&lt;br&gt;
  return () =&amp;gt; window.removeEventListener("scroll", onScroll);&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;function useScrollY(): number {&lt;br&gt;
  return useSyncExternalStore(&lt;br&gt;
    subscribe,&lt;br&gt;
    () =&amp;gt; window.scrollY,&lt;br&gt;
    () =&amp;gt; 0,                       // server snapshot&lt;br&gt;
  );&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;function Header() {&lt;br&gt;
  const y = useScrollY();&lt;br&gt;
  return &lt;/p&gt; 20 ? "scrolled" : ""}&amp;gt;{y};&lt;br&gt;
}&lt;br&gt;
&lt;code&gt;useSyncExternalStore&lt;/code&gt; exists because a manual &lt;code&gt;useEffect&lt;/code&gt; subscription can tear: two components reading the same store can render with different values in one commit. For browser APIs, third-party state containers, and anything with a &lt;code&gt;subscribe&lt;/code&gt; function, this hook is the supported pattern; the effect-based version is a fallback for stores that only offer an event emitter.

&lt;h2&gt;
  
  
  Common Compilation/Runtime Mistakes
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Symptom&lt;/th&gt;
&lt;th&gt;Cause&lt;/th&gt;
&lt;th&gt;Fix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Effect runs on every render&lt;/td&gt;
&lt;td&gt;Missing or unstable dependency&lt;/td&gt;
&lt;td&gt;Add primitives, memoize objects, or use []&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Effect never re-runs when a value changes&lt;/td&gt;
&lt;td&gt;Value omitted from deps&lt;/td&gt;
&lt;td&gt;Add it; do not silence the lint&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Old value inside the effect&lt;/td&gt;
&lt;td&gt;Stale closure over a previous render&lt;/td&gt;
&lt;td&gt;Add the dep, or use an updater function&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Infinite fetch loop&lt;/td&gt;
&lt;td&gt;Effect sets state that is also a dep&lt;/td&gt;
&lt;td&gt;Depend on inputs, not outputs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;async callback warning&lt;/td&gt;
&lt;td&gt;Effect returned a Promise&lt;/td&gt;
&lt;td&gt;Declare async function run() inside and call it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Race: wrong data after fast switching&lt;/td&gt;
&lt;td&gt;No cleanup&lt;/td&gt;
&lt;td&gt;AbortController or an ignore flag&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Memory leak warning on unmount&lt;/td&gt;
&lt;td&gt;Subscription without cleanup&lt;/td&gt;
&lt;td&gt;Return the unsubscribe function&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Layout shift on first paint&lt;/td&gt;
&lt;td&gt;useEffect measuring the DOM&lt;/td&gt;
&lt;td&gt;useLayoutEffect for measurements&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Effect runs twice in development&lt;/td&gt;
&lt;td&gt;StrictMode intentional double invoke&lt;/td&gt;
&lt;td&gt;Make cleanup complete; do not disable StrictMode&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;State set in an effect never appears&lt;/td&gt;
&lt;td&gt;Effect depends on a value that changes each render&lt;/td&gt;
&lt;td&gt;Fix deps; or compute during render&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The deepest trap is the one that produces no error: an effect whose dependency array is correct but whose cleanup is incomplete. React calls cleanup before the next run and on unmount, so any resource acquired in the effect — listener, timer, socket, abort controller, subscription — needs a matching release. If you cannot write the cleanup, the thing you are doing probably belongs in an event handler instead.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Kubernetes Requests vs Limits: CPU Throttling, QoS Classes, and OOMKilled</title>
      <dc:creator>R (RYUSLOG DEV)</dc:creator>
      <pubDate>Mon, 16 Nov 2026 09:51:07 +0000</pubDate>
      <link>https://dev.to/rshday/kubernetes-requests-vs-limits-cpu-throttling-qos-classes-and-oomkilled-517h</link>
      <guid>https://dev.to/rshday/kubernetes-requests-vs-limits-cpu-throttling-qos-classes-and-oomkilled-517h</guid>
      <description>&lt;h2&gt;
  
  
  Quick Summary
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;requests&lt;/code&gt; is what the scheduler reserves for a container; &lt;code&gt;limits&lt;/code&gt; is what the kernel enforces at runtime. They are different mechanisms with different consequences: the scheduler sums requests to decide whether the pod fits on a node, and the cgroup enforces limits afterwards. A container with a CPU limit is throttled by the CFS (Completely Fair Scheduler) quota when it exceeds it — it is never killed. A container with a memory limit that exceeds it is killed with &lt;code&gt;OOMKilled&lt;/code&gt; (exit code 137). That asymmetry is the whole reason CPU limits are contested and memory limits are not.&lt;/p&gt;

&lt;p&gt;The ratio between requests and limits also defines the pod's QoS class. &lt;code&gt;Guaranteed&lt;/code&gt; requires every container to have CPU and memory requests equal to limits; &lt;code&gt;BestEffort&lt;/code&gt; requires none of them; anything in between is &lt;code&gt;Burstable&lt;/code&gt;. QoS decides the eviction order under node pressure — BestEffort pods go first, then Burstable pods exceeding their requests, and Guaranteed last — which is why a critical service should be Guaranteed or at least have accurate requests.&lt;/p&gt;

&lt;p&gt;Getting these numbers right is empirical. Requests too high cause &lt;code&gt;Pending&lt;/code&gt; pods on a node with plenty of unused capacity (because "used" is measured against requests); memory requests too low let a node overcommit until the OOM killer picks victims. This guide covers the fields, the enforcement mechanics, QoS classes and eviction, LimitRange defaults and quotas, and the measurement loop — &lt;code&gt;kubectl top&lt;/code&gt;, CFS throttling metrics, OOM events — that turns guesses into values.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Syntax &amp;amp; Core Signatures
&lt;/h2&gt;

&lt;p&gt;yaml&lt;br&gt;
containers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;name: api
image: ghcr.io/acme/api:1.4.2
resources:
  requests:
    cpu: 250m           # 0.25 CPU; 1000m = 1 core
    memory: 512Mi
    ephemeral-storage: 1Gi
  limits:
    cpu: "1"            # no throttling below 1 core
    memory: 768Mi       # OOMKilled above this
yaml
# LimitRange: defaults and bounds applied to pods in a namespace
apiVersion: v1
kind: LimitRange
metadata: { name: defaults, namespace: prod }
spec:
limits:

&lt;ul&gt;
&lt;li&gt;type: Container
default: { cpu: 500m, memory: 512Mi }
defaultRequest: { cpu: 100m, memory: 256Mi }
max: { cpu: "2", memory: 2Gi }
min: { cpu: 50m, memory: 64Mi }
yaml
# ResourceQuota: aggregate ceiling for a namespace
apiVersion: v1
kind: ResourceQuota
metadata: { name: prod, namespace: prod }
spec:
hard:
requests.cpu: "20"
requests.memory: 40Gi
limits.cpu: "40"
limits.memory: 80Gi
pods: "100"
bash
kubectl describe node | sed -n '/Allocated resources/,/Events/p'
kubectl top pod -n prod --sort-by=memory
kubectl top node
kubectl get pod api-1 -o jsonpath='{.status.qosClass}{"\n"}'
kubectl get pod api-1 -o jsonpath='{.status.containerStatuses[0].lastState.terminated}'
kubectl get events -n prod --field-selector reason=OOMKilling
kubectl get resourcequota,limitrange -n prod
Units: &lt;code&gt;cpu: 100m&lt;/code&gt; is 0.1 core, &lt;code&gt;cpu: 1&lt;/code&gt; is one core; memory accepts &lt;code&gt;Mi&lt;/code&gt;/&lt;code&gt;Gi&lt;/code&gt; (binary) and &lt;code&gt;M&lt;/code&gt;/&lt;code&gt;G&lt;/code&gt; (decimal) — &lt;code&gt;500M&lt;/code&gt; is 500,000,000 bytes, &lt;code&gt;500Mi&lt;/code&gt; is 524,288,000, and mixing them in requests and limits is a common source of surprise.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Key Comparison Table
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Resource&lt;/th&gt;
&lt;th&gt;Over request&lt;/th&gt;
&lt;th&gt;Over limit&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;CPU&lt;/td&gt;
&lt;td&gt;No enforcement; may use idle capacity&lt;/td&gt;
&lt;td&gt;Throttled by CFS quota (latency rises)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Memory&lt;/td&gt;
&lt;td&gt;No enforcement until node pressure&lt;/td&gt;
&lt;td&gt;OOMKilled, exit 137&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ephemeral storage&lt;/td&gt;
&lt;td&gt;No enforcement&lt;/td&gt;
&lt;td&gt;Pod evicted&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Used by scheduler&lt;/td&gt;
&lt;td&gt;Yes (sum of requests)&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Used by kubelet eviction&lt;/td&gt;
&lt;td&gt;Thresholds on node pressure&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;QoS class&lt;/th&gt;
&lt;th&gt;Condition&lt;/th&gt;
&lt;th&gt;Eviction order&lt;/th&gt;
&lt;th&gt;Typical use&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Guaranteed&lt;/td&gt;
&lt;td&gt;Every container: cpu and memory requests = limits&lt;/td&gt;
&lt;td&gt;Last&lt;/td&gt;
&lt;td&gt;Latency-critical services&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Burstable&lt;/td&gt;
&lt;td&gt;At least one request or limit set, not equal&lt;/td&gt;
&lt;td&gt;Second&lt;/td&gt;
&lt;td&gt;Most workloads&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;BestEffort&lt;/td&gt;
&lt;td&gt;No requests or limits at all&lt;/td&gt;
&lt;td&gt;First&lt;/td&gt;
&lt;td&gt;Batch, tests&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Situation&lt;/th&gt;
&lt;th&gt;What actually happens&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;CPU limit 200m, app needs 400m&lt;/td&gt;
&lt;td&gt;Throttled to 200m; requests slow, no crash&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Memory limit 512Mi, app needs 700Mi&lt;/td&gt;
&lt;td&gt;OOMKilled, container restarts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;No requests, no limits&lt;/td&gt;
&lt;td&gt;Scheduled anywhere, first to be evicted&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Requests &amp;gt; node allocatable&lt;/td&gt;
&lt;td&gt;Pending, FailedScheduling: Insufficient cpu&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Requests far below usage&lt;/td&gt;
&lt;td&gt;Node oversubscribed; eviction under pressure&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Practical Edge Cases &amp;amp; Deep Dives
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. CPU throttling: the latency problem with no error
&lt;/h3&gt;

&lt;p&gt;bash&lt;br&gt;
$ kubectl top pod -n prod | grep api&lt;br&gt;
api-6d9f   180m   640Mi&lt;/p&gt;

&lt;h1&gt;
  
  
  Throttling ratio, from the kubelet/cAdvisor metrics:
&lt;/h1&gt;

&lt;p&gt;container_cpu_cfs_throttled_periods_total / container_cpu_cfs_periods_total&lt;/p&gt;

&lt;h1&gt;
  
  
  0.42  -&amp;gt; 42% of 100ms periods hit the quota
&lt;/h1&gt;

&lt;p&gt;A CPU limit of &lt;code&gt;200m&lt;/code&gt; means 20 ms of CPU per 100 ms period. A request that needs 30 ms of CPU is paused for the rest of the period, so p99 latency jumps even though average CPU (&lt;code&gt;kubectl top&lt;/code&gt;) looks comfortable at 180m. Throttling leaves no event, no OOM, no restart — just slow responses, which is why it is the hardest limit problem to find. The remedies are to raise or remove the CPU limit (keeping the request for scheduling), or to accept it only where latency does not matter.&lt;/p&gt;

&lt;p&gt;Removing the CPU limit is safe when the request is accurate: the scheduler still reserves capacity, and the pod may use idle CPU on the node. The risk is a runaway process starving neighbours, which matters on shared nodes and not at all on dedicated ones.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. &lt;code&gt;OOMKilled&lt;/code&gt; and the JVM that ignores its limit
&lt;/h3&gt;

&lt;p&gt;bash&lt;br&gt;
$ kubectl get pod api-1 -o jsonpath='{.status.containerStatuses[0].lastState.terminated.reason}'&lt;br&gt;
OOMKilled&lt;br&gt;
$ kubectl get pod api-1 -o jsonpath='{.status.containerStatuses[0].lastState.terminated.exitCode}'&lt;br&gt;
137&lt;br&gt;
A JVM in a container historically read the host's memory and sized its heap from it, so a 512Mi limit with a host of 32 GiB produced a heap larger than the limit and an OOM kill within minutes. Modern JVMs are container-aware (&lt;code&gt;UseContainerSupport&lt;/code&gt;, default since 10) and use &lt;code&gt;-XX:MaxRAMPercentage&lt;/code&gt; (25% by default) of the cgroup limit — but a &lt;code&gt;-Xmx&lt;/code&gt; flag in the image's &lt;code&gt;JAVA_OPTS&lt;/code&gt; overrides it. The memory limit must also cover JVM non-heap: metaspace, code cache, thread stacks, direct buffers, and the GC itself, which in practice is 20-40% above the heap.&lt;/p&gt;

&lt;p&gt;yaml&lt;br&gt;
env:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;name: JAVA_TOOL_OPTIONS
value: "-XX:MaxRAMPercentage=65 -XX:InitialRAMPercentage=65"
resources:
requests: { memory: 1Gi }
limits:   { memory: 1Gi }
### 3. Requests and the &lt;code&gt;Pending&lt;/code&gt; pod on an idle-looking node&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;bash&lt;br&gt;
$ kubectl describe node worker-2 | sed -n '/Allocated resources/,/Events/p'&lt;br&gt;
Resource           Requests    Limits&lt;br&gt;
cpu                3850m (96%) 6000m (150%)&lt;br&gt;
memory             14Gi (88%)  20Gi (125%)&lt;br&gt;
$ kubectl top node worker-2&lt;br&gt;
worker-2  cpu: 900m (22%)  memory: 6Gi (38%)&lt;br&gt;
The scheduler compares requests to allocatable, not usage to capacity. A node at 96% requested with 22% used cannot accept another 250m pod. The fix is not to raise the node size but to correct the requests: sample actual usage, set requests near the p50-p80 of real consumption, and keep a limit for the ceiling. Overestimating requests "for safety" is the most common cause of wasteful clusters.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. QoS class as an availability decision
&lt;/h3&gt;

&lt;p&gt;yaml&lt;/p&gt;

&lt;h1&gt;
  
  
  Guaranteed: a critical service that must be evicted last
&lt;/h1&gt;

&lt;p&gt;resources:&lt;br&gt;
  requests: { cpu: "1", memory: 2Gi }&lt;br&gt;
  limits:   { cpu: "1", memory: 2Gi }&lt;br&gt;
Every container in the pod must satisfy equality for both cpu and memory, otherwise the pod is Burstable. A single sidecar without limits demotes the whole pod, so a service mesh injector that adds an unlimited proxy silently changes the pod's eviction class.&lt;/p&gt;

&lt;p&gt;Under memory pressure, the kubelet evicts BestEffort pods first, then Burstable pods using more than their requests, and Guaranteed last; within a class, the ranking uses usage relative to requests plus priority. A Guaranteed pod can still be OOM killed by the &lt;em&gt;kernel&lt;/em&gt; if it exceeds its own memory limit, so Guaranteed is protection against eviction, not against its own limit.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Quotas, LimitRanges, and the pod that is rejected, not unscheduled
&lt;/h3&gt;

&lt;p&gt;bash&lt;br&gt;
$ kubectl apply -f api.yaml&lt;br&gt;
Error from server (Forbidden): error when creating "api.yaml": pods "api-1" is forbidden:&lt;br&gt;
exceeded quota: prod, requested: requests.cpu=500m, used: requests.cpu=20, limited: requests.cpu=20&lt;br&gt;
A quota rejection happens at admission, before scheduling, and the message names the exceeded resource. A LimitRange turns a missing &lt;code&gt;resources&lt;/code&gt; block into defaults; without one, a container with no requests is BestEffort and counts as zero against &lt;code&gt;requests.*&lt;/code&gt; quotas, which is how a namespace can run at "0% quota" and still be full of pods. In a namespace with a quota on &lt;code&gt;requests.cpu&lt;/code&gt;, every pod needs explicit requests or a LimitRange default, otherwise admission fails with &lt;code&gt;must specify limits.cpu&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Measuring before setting numbers
&lt;/h3&gt;

&lt;p&gt;bash&lt;/p&gt;

&lt;h1&gt;
  
  
  Observed usage over time (metrics-server history is short; use Prometheus for real trends)
&lt;/h1&gt;

&lt;p&gt;kubectl top pod -n prod --containers | sort -k3 -h | tail -20&lt;/p&gt;

&lt;h1&gt;
  
  
  Peak memory from the container's own history
&lt;/h1&gt;

&lt;p&gt;kubectl get pod api-1 -o jsonpath-range='{range .status.containerStatuses[*]}{.name} {.lastState.terminated.reason}{"\n"}{end}'&lt;/p&gt;

&lt;h1&gt;
  
  
  Right-sizing from Prometheus
&lt;/h1&gt;

&lt;h1&gt;
  
  
  max_over_time(container_memory_working_set_bytes{namespace="prod"}[7d]) by (pod)
&lt;/h1&gt;

&lt;h1&gt;
  
  
  quantile_over_time(0.95, rate(container_cpu_usage_seconds_total[5m])[7d:]) by (pod)
&lt;/h1&gt;

&lt;p&gt;Set memory requests at the observed peak working set plus a margin, memory limits at the peak plus headroom for bursts, CPU requests at the p95 usage, and CPU limits either absent or well above p99. &lt;code&gt;container_memory_working_set_bytes&lt;/code&gt; is the figure the OOM killer effectively acts on, not &lt;code&gt;container_memory_usage_bytes&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Runtime &amp;amp; Configuration Mistakes
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Symptom&lt;/th&gt;
&lt;th&gt;Cause&lt;/th&gt;
&lt;th&gt;Fix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;OOMKilled, exit 137&lt;/td&gt;
&lt;td&gt;Memory limit below working set&lt;/td&gt;
&lt;td&gt;Raise the limit; fix heap/MaxRAMPercentage&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Slow responses, no errors, low CPU usage&lt;/td&gt;
&lt;td&gt;CPU throttling&lt;/td&gt;
&lt;td&gt;Raise or remove the CPU limit&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pending: Insufficient cpu&lt;/td&gt;
&lt;td&gt;Requests exceed allocatable&lt;/td&gt;
&lt;td&gt;Lower requests, add nodes, check quotas&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Node "full" at 20% usage&lt;/td&gt;
&lt;td&gt;Requests overestimated&lt;/td&gt;
&lt;td&gt;Right-size from measurements&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pod rejected on create&lt;/td&gt;
&lt;td&gt;Namespace quota exhausted&lt;/td&gt;
&lt;td&gt;Raise the quota or request less&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;must specify limits.cpu&lt;/td&gt;
&lt;td&gt;Quota requires limits, none set&lt;/td&gt;
&lt;td&gt;Set limits or add a LimitRange&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Container gets no default resources&lt;/td&gt;
&lt;td&gt;No LimitRange&lt;/td&gt;
&lt;td&gt;Add defaultRequest/default&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Critical pod evicted first&lt;/td&gt;
&lt;td&gt;BestEffort or low requests&lt;/td&gt;
&lt;td&gt;Set requests=limits (Guaranteed), priority&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JVM heap ignores the limit&lt;/td&gt;
&lt;td&gt;-Xmx overrides container awareness&lt;/td&gt;
&lt;td&gt;Remove -Xmx, use MaxRAMPercentage&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sidecar demotes the pod to Burstable&lt;/td&gt;
&lt;td&gt;Sidecar without resources&lt;/td&gt;
&lt;td&gt;Set resources on every container&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Memory grows until the node OOMs&lt;/td&gt;
&lt;td&gt;No memory limit anywhere&lt;/td&gt;
&lt;td&gt;Set limits, monitor working set&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ephemeral storage eviction&lt;/td&gt;
&lt;td&gt;No ephemeral-storage request/limit&lt;/td&gt;
&lt;td&gt;Set both; clean logs and temp files&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Requests and limits are two different promises: requests tell the scheduler what to reserve and decide QoS, limits tell the kernel what to enforce and decide whether the symptom is throttling or death. Measure the working set, set memory requests at the real peak with a limit slightly above it, keep CPU requests accurate and CPU limits loose or absent, and check every container in the pod — because one unset sidecar changes the class of the whole workload.&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>devops</category>
      <category>performance</category>
      <category>cloud</category>
    </item>
    <item>
      <title>Fifteen developer tools, zero dependencies</title>
      <dc:creator>R (RYUSLOG DEV)</dc:creator>
      <pubDate>Mon, 16 Nov 2026 09:51:02 +0000</pubDate>
      <link>https://dev.to/rshday/fifteen-developer-tools-zero-dependencies-2ef7</link>
      <guid>https://dev.to/rshday/fifteen-developer-tools-zero-dependencies-2ef7</guid>
      <description>&lt;h1&gt;
  
  
  Fifteen developer tools, zero dependencies
&lt;/h1&gt;

&lt;p&gt;Most "online JSON formatter" pages send your payload to a server, keep it for a while, and hope you don't think about it. Building tools that don't need to is mostly a matter of knowing which platform APIs already exist, and being willing to write the small amount of maths that a library would otherwise hide.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.ryuslog.com/tools" rel="noopener noreferrer"&gt;ryuslog.com/tools&lt;/a&gt; now has fifteen of them — JSON, YAML, diff, regex, SQL, CSV, JWT, Base64, URL, hash, cURL, UUID, timestamps, cron and colour/contrast. All of them run entirely in the tab: no upload, no account, no server round trip. This is what that actually takes, and where the sharp edges are.&lt;/p&gt;

&lt;h2&gt;
  
  
  The rule that shapes everything
&lt;/h2&gt;

&lt;p&gt;Every tool is one pure function from input to output, with the DOM kept at the edges:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;ToolResult&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;formatJson&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;opts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;JsonOptions&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ToolResult&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* … */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That shape buys three things at once. The engine is testable without a browser, the UI is a thin shell that renders &lt;code&gt;ToolResult&lt;/code&gt;, and an error carries a position so the editor can underline the offending character instead of saying "invalid input".&lt;/p&gt;

&lt;p&gt;The rest is browser API archaeology.&lt;/p&gt;

&lt;h2&gt;
  
  
  Web Crypto: generous, with two gaps
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;crypto.subtle.digest&lt;/code&gt; covers SHA-1, SHA-256, SHA-384 and SHA-512, is fast, and returns an &lt;code&gt;ArrayBuffer&lt;/code&gt; you hex-encode yourself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subtle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'SHA-256'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TextEncoder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Uint8Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;padStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two gaps matter for developer tooling:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;MD5 is not there.&lt;/strong&gt; Plenty of legacy checksums still need it, so the &lt;a href="https://www.ryuslog.com/tools/hash" rel="noopener noreferrer"&gt;hash generator&lt;/a&gt; computes MD5 in plain TypeScript and uses &lt;code&gt;subtle&lt;/code&gt; for the SHA family.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nothing is timing-safe for you.&lt;/strong&gt; Verifying an HMAC (as a JWT signature check would) is &lt;code&gt;importKey&lt;/code&gt; + &lt;code&gt;sign&lt;/code&gt; + a &lt;strong&gt;byte-wise&lt;/strong&gt; comparison — &lt;code&gt;===&lt;/code&gt; on hex strings leaks timing, and comparing base64url strings is worse because of padding.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is also a third gap that trips people up: &lt;code&gt;subtle&lt;/code&gt; is only available in a secure context, so the same code silently fails on &lt;code&gt;http://192.168.x.x&lt;/code&gt; during development.&lt;/p&gt;

&lt;h2&gt;
  
  
  OKLCH in about forty lines
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://www.ryuslog.com/tools/color" rel="noopener noreferrer"&gt;colour tool&lt;/a&gt; converts HEX → RGB → HSL → OKLCH and back, and checks WCAG contrast. OKLCH is where the maths is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;toLinear&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mf"&gt;0.04045&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;12.92&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.055&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mf"&gt;1.055&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt; &lt;span class="mf"&gt;2.4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// linear sRGB -&amp;gt; Oklab&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.4122214708&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.5363325363&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.0514459929&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.2119034982&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.6806995451&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.1073969566&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0883024619&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.2817188376&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.6299787005&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;l_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cbrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cbrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cbrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;L&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.2104542553&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;l_&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.7936177850&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;m_&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mf"&gt;0.0040720468&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;s_&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1.9779984951&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;l_&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mf"&gt;2.4285922050&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;m_&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.4505937099&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;s_&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;bb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.0259040371&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;l_&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;0.7827717662&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;m_&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mf"&gt;0.8086757660&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;s_&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;C&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hypot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;H&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;atan2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;180&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PI&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things that only show up when you build the inverse:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Round trips leave the gamut.&lt;/strong&gt; OKLCH can express colours sRGB cannot, so converting back produces components outside &lt;code&gt;[0, 1]&lt;/code&gt;. Clamping each channel shifts the hue; reducing chroma at constant lightness is the honest fix, and the UI needs to say when it happened.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Contrast is not computed on the hex.&lt;/strong&gt; WCAG's ratio is &lt;code&gt;(L1 + 0.05) / (L2 + 0.05)&lt;/code&gt; over &lt;em&gt;relative luminance&lt;/em&gt;, which is the linear-light value weighted &lt;code&gt;0.2126 R + 0.7152 G + 0.0722 B&lt;/code&gt;. Skipping the linearisation is the classic bug: it passes pairs that fail AA in a real browser. And if a colour has alpha, composite it over the background first.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Cron: the next run time is not "now plus five minutes"
&lt;/h2&gt;

&lt;p&gt;Explaining &lt;code&gt;*/15 9-17 * * 1-5&lt;/code&gt; in English is easy. Producing the next six run times is where implementations diverge, because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;day-of-month and day-of-week combine with &lt;strong&gt;OR&lt;/strong&gt;, not AND, when both are restricted;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0&lt;/code&gt; and &lt;code&gt;7&lt;/code&gt; are both Sunday;&lt;/li&gt;
&lt;li&gt;February 30th has to be skipped, not incremented;&lt;/li&gt;
&lt;li&gt;a DST jump can make a wall-clock time not exist, or exist twice.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Iterating minute by minute for up to a year is 525,600 checks — fine for one expression, wasteful for six results. Iterating the &lt;em&gt;sets&lt;/em&gt; (month → day → hour → minute) is both faster and easier to keep correct. Showing the answer in local time &lt;strong&gt;and&lt;/strong&gt; UTC is the cheapest way to defuse "but it runs at 3am".&lt;/p&gt;

&lt;p&gt;Try it on &lt;a href="https://www.ryuslog.com/tools/cron" rel="noopener noreferrer"&gt;the cron parser&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Diff: trim first, then spend the budget
&lt;/h2&gt;

&lt;p&gt;A textbook LCS table for two 5,000-line files is 25 million cells. Before any of that, trim the common prefix and suffix — on a config diff, that's usually 90% of the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;endA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;endB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;endA&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;endB&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;endA&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;endB&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;endA&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;endB&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What remains is the actual edit region, where the LCS table is small. When it isn't, the choices are Myers' O(ND) algorithm (fast when the change is small, quadratic when it isn't) or anchoring on unique lines first, as patience diff does. The &lt;a href="https://www.ryuslog.com/tools/diff" rel="noopener noreferrer"&gt;diff tool&lt;/a&gt; also ignores whitespace on request, which is the single most useful option in code review.&lt;/p&gt;

&lt;h2&gt;
  
  
  JSON → TypeScript, with optionality
&lt;/h2&gt;

&lt;p&gt;Inferring interfaces is a recursion with a merge step. The interesting part is what to do with arrays of objects that differ:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'object'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;seen&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;number&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'array'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Shape&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'scalar'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Merge each object into the accumulator, count how many elements contained each key, and mark a key optional when it appears in fewer than all of them. &lt;code&gt;null&lt;/code&gt; becomes &lt;code&gt;| null&lt;/code&gt;, numbers stay &lt;code&gt;number&lt;/code&gt; (unless the literal set is small and closed), and keys that aren't valid identifiers get quoted. The &lt;a href="https://www.ryuslog.com/tools/json" rel="noopener noreferrer"&gt;JSON formatter&lt;/a&gt; emits the result with nested interfaces named from the path, because &lt;code&gt;Data&lt;/code&gt;, &lt;code&gt;Data2&lt;/code&gt;, &lt;code&gt;Data3&lt;/code&gt; is what makes generated types useless.&lt;/p&gt;

&lt;h2&gt;
  
  
  cURL → code is a shell problem, not an HTTP one
&lt;/h2&gt;

&lt;p&gt;Half of that tool is tokenising: &lt;code&gt;\&lt;/code&gt; line continuations, &lt;code&gt;'single quoted'&lt;/code&gt; vs &lt;code&gt;"double quoted $VAR"&lt;/code&gt;, &lt;code&gt;-H 'A: b'&lt;/code&gt; vs &lt;code&gt;-H'A: b'&lt;/code&gt;. Only then do you map flags — &lt;code&gt;-X&lt;/code&gt;, &lt;code&gt;--data-raw&lt;/code&gt;, &lt;code&gt;--data-urlencode&lt;/code&gt;, &lt;code&gt;-u&lt;/code&gt;, &lt;code&gt;--cookie&lt;/code&gt;, &lt;code&gt;-F&lt;/code&gt;, &lt;code&gt;--compressed&lt;/code&gt; — and emit &lt;code&gt;fetch&lt;/code&gt;, Python &lt;code&gt;requests&lt;/code&gt;, or Go &lt;code&gt;net/http&lt;/code&gt;. Getting &lt;code&gt;--data-urlencode&lt;/code&gt; wrong turns a form body into a JSON body and the generated snippet silently fails, which is exactly the bug people use such a tool to avoid.&lt;/p&gt;

&lt;h2&gt;
  
  
  The UI details that decide whether the tab stays open
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Run on input, debounced.&lt;/strong&gt; 120 ms feels instant and keeps typing smooth; a "Run" button feels like 1998 and hides the feedback loop.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Errors carry a position.&lt;/strong&gt; &lt;code&gt;Unexpected token } in JSON at position 42&lt;/code&gt; is useless without underlining character 42.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;State in the URL hash.&lt;/strong&gt; &lt;code&gt;#json?input=…&lt;/code&gt; makes a result shareable and survives a reload; keep it out of server logs, which the hash does by construction.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No layout shift.&lt;/strong&gt; Reserve the output pane's height before the first result; jumping content is what makes a tool feel broken.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Contrast in the UI itself.&lt;/strong&gt; A contrast checker with unreadable labels is a bad joke, and dark mode needs its own pass.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What the platform still doesn't do for you
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;navigator.clipboard.writeText&lt;/code&gt; needs a secure context &lt;strong&gt;and&lt;/strong&gt; a user gesture; anything else falls back to a hidden textarea and &lt;code&gt;document.execCommand('copy')&lt;/code&gt;. Downloads are a &lt;code&gt;Blob&lt;/code&gt; plus &lt;code&gt;URL.createObjectURL&lt;/code&gt;, and you must &lt;code&gt;revokeObjectURL&lt;/code&gt; or leak the file for the life of the tab. And "no server" means no history and no sync — &lt;code&gt;localStorage&lt;/code&gt; is the only memory you get, so treat it as a convenience, never as storage.&lt;/p&gt;

&lt;p&gt;Fifteen tools later, the thing I'd tell someone starting the same project: the browser gives you more than you think (crypto, URL, Intl, structured clone, workers) and less than you hope (no MD5, no timing-safe compare, no diff, no colour science). Both halves are small, and neither needs a dependency.&lt;/p&gt;

&lt;p&gt;The tools are at &lt;a href="https://www.ryuslog.com/tools" rel="noopener noreferrer"&gt;ryuslog.com/tools&lt;/a&gt;; longer write-ups on the same site go deeper on the specifics, for example &lt;a href="https://www.ryuslog.com/blog/kubernetes-resource-requests-limits-qos-classes-and-oomkilled" rel="noopener noreferrer"&gt;resource requests, limits and OOMKilled&lt;/a&gt; and &lt;a href="https://www.ryuslog.com/blog/react-useeffect-dependencies-cleanup-and-race-conditions" rel="noopener noreferrer"&gt;useEffect dependencies and race conditions&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>typescript</category>
      <category>javascript</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
