# Svelte vs React: one component, two ways

- Published: 2026-12-02
- Tags: Why Svelte, Svelte
- URL: https://devtrillo.com/writing/why-svelte-19-svelte-vs-react-one-component-two-ways/

> Part 19 of a series on Svelte in 2026. [Part 18](/writing/why-svelte-18-observability-in-sveltekit/) traced a running SvelteKit app. This part steps back from SvelteKit and puts one component next to its React twin.

Every earlier part compared one idea with React: a counter, a derived value, an effect, a prop. That's fair to each idea and unfair to both frameworks, because nobody ships a counter. So for this part I wrote one ordinary component twice and counted what ran.

It's a packing list. You type an item and add it, tick items off as you pack them, see "3 of 7 packed", hide what's already packed, and remove things you changed your mind about. Two components in each version: the list, and a row. I loaded each production build with 100 items in Chromium and put a counter at the top of every component function.

Checking one item off ran 101 component functions in React: the list, then all 100 rows. With the React Compiler turned on it was still 101. In Svelte it was zero. Getting React down to two took three changes, and the reason it took them is the most useful thing I learned from the exercise.

The lab ran Svelte 5.57.3, React 19.3.1 and `babel-plugin-react-compiler` 1.0.0, all built with Vite 8.3.5 and driven by Playwright 1.64.1.

## The Svelte version

```svelte title="PackingList.svelte"
<script lang="ts">
  import PackingItem from "./PackingItem.svelte";
  import { seed } from "./seed";

  let items = $state(seed());
  let draft = $state("");
  let hidePacked = $state(false);

  let packed = $derived(items.filter((i) => i.packed).length);
  let shown = $derived(hidePacked ? items.filter((i) => !i.packed) : items);

  function add(event: SubmitEvent) {
    event.preventDefault();
    const name = draft.trim();
    if (!name) return;
    items.push({ id: Date.now(), name, packed: false });
    draft = "";
  }

  function remove(id: number) {
    items = items.filter((i) => i.id !== id);
  }
</script>

<form onsubmit={add}>
  <input bind:value={draft} placeholder="Add an item" />
  <button disabled={!draft.trim()}>Add</button>
</form>

<p>{packed} of {items.length} packed</p>
<label><input type="checkbox" bind:checked={hidePacked} /> Hide packed</label>

<ul>
  {#each shown as item (item.id)}
    <PackingItem
      {item}
      ontoggle={() => (item.packed = !item.packed)}
      onremove={() => remove(item.id)}
    />
  {/each}
</ul>

<style>
  ul {
    list-style: none;
    padding: 0;
  }
</style>
```

`PackingItem.svelte` takes `item`, `ontoggle` and `onremove` as props and renders an `<li>` with a checkbox, the name and a remove button, plus a scoped `.packed` style that strikes the name through.

Nothing in there would surprise someone who read parts 3 to 6. `items.push(...)` is the whole update, because `$state` made the array a deep proxy. `packed` and `shown` say what they are and never list what they read. Ticking a box flips one property on one object, and the row's class and the count are the only two things on the page that read it.

## The React version

```tsx title="PackingList.tsx"
import { useState, type FormEvent } from "react";
import { PackingItem } from "./PackingItem";
import { seed } from "./seed";

export function PackingList() {
  const [items, setItems] = useState(seed);
  const [draft, setDraft] = useState("");
  const [hidePacked, setHidePacked] = useState(false);

  const packed = items.filter((i) => i.packed).length;
  const shown = hidePacked ? items.filter((i) => !i.packed) : items;

  function add(event: FormEvent) {
    event.preventDefault();
    const name = draft.trim();
    if (!name) return;
    setItems([...items, { id: Date.now(), name, packed: false }]);
    setDraft("");
  }

  function toggle(id: number) {
    setItems(items.map((i) => (i.id === id ? { ...i, packed: !i.packed } : i)));
  }

  function remove(id: number) {
    setItems(items.filter((i) => i.id !== id));
  }

  return (
    <>
      <form onSubmit={add}>
        <input
          value={draft}
          onChange={(e) => setDraft(e.target.value)}
          placeholder="Add an item"
        />
        <button disabled={!draft.trim()}>Add</button>
      </form>

      <p>
        {packed} of {items.length} packed
      </p>
      <label>
        <input
          type="checkbox"
          checked={hidePacked}
          onChange={(e) => setHidePacked(e.target.checked)}
        />{" "}
        Hide packed
      </label>

      <ul className="list">
        {shown.map((item) => (
          <PackingItem
            key={item.id}
            item={item}
            onToggle={() => toggle(item.id)}
            onRemove={() => remove(item.id)}
          />
        ))}
      </ul>
    </>
  );
}
```

The row is the same shape as Svelte's, with its `.packed` class coming from a CSS Module, since React has no scoped styles of its own (part 11 covered that).

This is the code I'd write without thinking about performance, and I think most React developers would too. It's longer: formatted by Prettier, Svelte's two files came to 61 non-blank lines and React's three to 76, before the list got any styles. Most of the gap is `toggle`: in React you build a new array with a new object in it, while Svelte assigns to `item.packed`.

The bundles differ more than the source does. Svelte's build was 17.2 kB gzipped and React's 69.4 kB, 70.3 kB with the Compiler. Part 2 explains where that comes from; it's mostly React's fixed runtime, not this component.

## What ran when one thing changed

**What ran when one thing changed** (component functions, 100 items). With 100 items on the list, you…

| Change | React, as written | React + Compiler | React + Compiler, reshaped, memo() | Svelte | What to notice |
| --- | --- | --- | --- | --- | --- |
| check one item off | 101 (list + 100 rows) | 101 (list + 100 rows) | 2 (list + 1 row) | 0 | The Compiler alone didn't help: toggle() reads items, so every row got a new onToggle and missed its cache. |
| type one letter into the add box | 101 (list + 100 rows) | 1 (list) | 1 (list) | 0 | The case the Compiler is built for. The rows' JSX didn't depend on draft, so it was reused. |
| add an item | 102 (list + 101 rows) | 102 (list + 101 rows) | 2 (list + the new row) | 1 (the new row) | Svelte's 1 is the new PackingItem being created. Nothing else ran. |
| tick Hide packed | 101 (list + 100 rows) | 101 (list + 100 rows) | 1 (list) | 0 | Hiding one packed row means removing one <li>. Svelte's keyed each block did only that. |
| remove an item | 101 (list + 100 rows) | 101 (list + 100 rows) | 1 (list) | 0 | Same story as hiding: one row leaves, and only the reshaped React version left the others alone. |

The plain React column is React's model working as documented: state changed in `PackingList`, so `PackingList` ran again, and so did every child it rendered. Typing one letter into the "add" box ran 100 row functions that had nothing to do with the draft.

The React Compiler fixed exactly that case. It memoized the mapped rows on `shown`, `toggle` and `remove`, none of which change while you type, so the list ran once and the rows not at all. That's the "skipping cascading re-rendering" the Compiler docs promise.[^compiler]

Checking an item off was still 101, and I had to read the compiled output to see why. `toggle` reads `items`, so the Compiler rebuilt it whenever `items` changed, and so it rebuilt every `() => toggle(item.id)` arrow, so every row got a new `onToggle` prop and missed its cache. The docs walk through a similar list and say the Compiler handles it "with or without the arrow function". That held in my lab while I typed, when the parent re-rendered for an unrelated reason. When the array itself changes, as it does every time you tick a box, every arrow is new.

Getting down to two took three changes:

1. `setItems((items) => items.map(...))`, the updater form, so `toggle` no longer reads `items` and stays the same function.
2. Pass `toggle` itself to the row, which calls `onToggle(item.id)`, instead of a fresh arrow per row.
3. Wrap the row in `memo()`.

With the first two and no `memo()`, the row functions still ran 100 times, but each one hit its compiled cache and returned its old JSX. That's cheaper than a real re-render without being free. Without the Compiler, `memo()` alone did nothing, since `toggle` was a new function on every render.

Does any of that matter? At 100 items, no. I timed one keystroke and one toggle, from the event to the next task (JavaScript and DOM updates, not paint), and every version stayed under half a millisecond. At 5,000 items, plain React spent about 14 ms per keystroke and 17 ms per toggle, which is most of a 60 Hz frame. The Compiler took the keystroke to 0.1 ms and left the toggle at 17. The reshaped version with the cache hits and no `memo()` toggled in 3.6 ms; with `memo()`, 1.3 ms. Svelte, as written, did both in about 0.1 and 2 ms.

Svelte lost that last one. Its 2 ms is the `packed` count re-running over 5,000 items. The memoized React version re-counts the same 5,000, so my guess is the difference is reading through Svelte's proxies instead of plain objects; I didn't profile it.

**Quiz:** With the React Compiler on, you add a 'sort by name' checkbox to the React list. Ticking it re-sorts shown. How many row functions run?

- None. The rows' props didn't change, and the Compiler caches the rows.
- All of them, unless the rows are memo() and get a stable onToggle
- Only the rows that changed position

**Answer:** All of them, unless the rows are memo() and get a stable onToggle — Same as ticking an item: a new array means a new map, new arrow props, and a cache miss in every row. With the reshaped version, React only moves the rows.

- Not "None. The rows' props didn't change, and the Compiler caches the rows.": The Compiler cached the mapped rows on shown. A sort gives you a new shown array, so the map runs again and creates new arrows for every row.
- Not "Only the rows that changed position": React doesn't decide what to run by position. A keyed list lets it move DOM nodes, but the row functions still run if their props are new.

## Where the Svelte version bit me

My first Svelte row didn't take an `ontoggle` callback. It did this:

```svelte title="PackingItem.svelte"
<input type="checkbox" bind:checked={item.packed} />
```

That's two fewer props, and it works: the box ticks, and the count updates. In dev, it also logged `ownership_invalid_mutation`: "Mutating unbound props (`item`, …) is strongly discouraged." Svelte suggests `bind:item` in the parent, or a callback.

I tried `bind:item` first, and that didn't compile: `each_item_invalid_assignment`, because you can't bind to the item of an `{#each}` in runes mode. Binding through an index would need `items[i]`, and I was iterating over `shown`, a filtered copy. So I wrote the callback, and the Svelte row ended up looking like the React row: a prop for the data and a function to call when it should change.

That's the honest shape of Svelte's shortcut. Mutation is the easy path inside the component that owns the state, and Svelte asks you to stop at the component boundary. React never lets you start, since `setItems` with a new array is the only way to change anything, and that's exactly why `memo()` can trust a shallow compare: a row's `item` is a different object if and only if it changed.

## Where React's version is better

It's all JavaScript. `shown.map(...)` is a call you already know, `PackingItem` could live in the same file as the list if I wanted, and TypeScript checks every expression in the markup without a language server that understands a template syntax. Svelte has snippets for the "small thing in the same file" case and svelte-check for the types, and both are good, but they're Svelte's to learn.

The rest is the reason part 1 gave: React is used by 85% of State of JS 2025 respondents against Svelte's 27.5%.[^sojs] Every library you'll need has a React version first, and every developer you hire has written `useState`. A packing list doesn't change that.

**Quiz:** You switch the Svelte row back to bind:checked={item.packed}. What happens in production?

- The build fails with ownership_invalid_mutation
- It works. The warning only shows in dev, and the box ticks and the count updates.
- The checkbox ticks but the count doesn't update

**Answer:** It works. The warning only shows in dev, and the box ticks and the count updates. — The mutation goes through the same proxy either way. The warning is Svelte telling you the parent no longer controls who changes its data.

- Not "The build fails with ownership_invalid_mutation": It's a dev-time warning, not a compile error. The build passes.
- Not "The checkbox ticks but the count doesn't update": item is the same proxy the parent's $derived reads, so the count updates.

## What I'd take from it

Svelte's version was shorter and did the least work without my thinking about it. React's version did more, and the Compiler took care of half of that for free; the other half needed me to know how the Compiler caches, which is the thing it was supposed to save me from knowing. That's the real difference, more than the line count: in Svelte the fast version is the version you write first, and in React it's a version you can reach.

At 100 items, though, nobody will feel any of this. Pick on the other grounds: the team, the libraries, the hiring pool. The verdict on those is part 22's job.

Next: building a component library in Svelte.

[^compiler]: React docs, [Introduction to React Compiler](https://react.dev/learn/react-compiler/introduction), "What does React Compiler do?" and the `ExpensiveComponent` example.

[^sojs]: [State of JS 2025: Front-end frameworks](https://2025.stateofjs.com/en-US/libraries/front-end-frameworks/), usage figures pulled from the Devographics API as described in part 1. The [runes announcement](https://svelte.dev/blog/runes) covers why Svelte 5 moved to signals in the first place.

- **Part 20 — A component library in Svelte**: Packaging Svelte components for other apps to use
