<?xml version="1.0" encoding="utf-8"?>
    <feed xmlns="http://www.w3.org/2005/Atom">
      <id>https://wolfgirl.dev/blog/</id>
      <title>PolyWolf's Blog - #javascript</title>
      <subtitle
        >all posts tagged "javascript" on a blog written by PolyWolf</subtitle
      >
      <icon>/apple-touch-icon.png</icon>
      <link href="/tags/javascript/rss.xml" rel="self" />
      <link href="https://wolfgirl.dev/blog/" />
      <updated>2025-03-20T16:56:15Z</updated>
      
    <entry>
      <title><![CDATA[Quick Thoughts On: React vs SolidJS]]></title>
      <link rel="alternate" href="https://wolfgirl.dev/blog/2025-03-20-quick-thoughts-on-react-vs-solidjs/" />
      <id>https://wolfgirl.dev/blog/2025-03-20-quick-thoughts-on-react-vs-solidjs/</id>
      <published>2025-03-20T16:56:15Z</published>
      <updated>2025-03-20T16:56:15Z</updated>
      <summary><![CDATA[TL;DR just use React, or Preact if you care about bundle size.  ---  A while ago, I [wrote my own post composer](https://wolfgirl.dev...]]></summary>
      <content type="html"><![CDATA[<p>TL;DR just use React, or Preact if you care about bundle size.</p><hr><p>A while ago, I <a href=https://wolfgirl.dev/blog/2024-11-08-what-if-anyone-could-use-my-post-composer-/>wrote my own post composer</a>, which I use to make blog posts from my phone, including this one. It's pretty nice!</p><p>One of the requirements I had when making it was "I want my bundle sizes to be as small as possible". Inspired by seeing my work's 66MB React app, I decided to look for other frontend frameworks, still reactive (because that <em>is</em> just the best paradigm), but smaller & faster.</p><p>Eventually, I settled on SolidJS. After having used it for a while on more than just a simple "Hello, World!" project, I can confidently claim there's a pretty fatal (for me) flaw:</p><h2 id=heading-solidjs-defaults-to-not-re-rendering-when-values-change>SolidJS defaults to <em>not</em> re-rendering when values change<a aria-label="Link to heading 'SolidJS defaults to not re-rendering when values change'" data-heading-content="SolidJS defaults to not re-rendering when values change" class=anchor href=#solidjs-defaults-to-not-re-rendering-when-values-change></a></h2><p>From my understanding, this is how SolidJS works: everything is based around the "signal" as the underlying reactive primitive, with function calls as the way to subscribe to those signals:</p><pre class=syntax-highlighting><code class=language-jsx><span class=keyword>export</span> <span class=keyword>const</span> <span class=constructor>Component</span> <span class=operator>=</span> <span class=punctuation>(</span><span class=variable>props</span><span class=punctuation>)</span> <span class=operator>=></span> <span class=punctuation>{</span>
  <span class=keyword>const</span> <span class=punctuation>[</span><span class=variable>name</span><span class=punctuation>,</span> <span class=variable>setName</span><span class=punctuation>]</span> <span class=operator>=</span> <span class=function>createSignal</span><span class=punctuation>(</span><span class=string>""</span><span class=punctuation>);</span>
  <span class=keyword>const</span> <span class=function>title</span> <span class=operator>=</span> <span class=punctuation>()</span> <span class=operator>=></span> <span class=string>`</span><span class=punctuation>${</span><span class=variable>props</span><span class=punctuation>.</span><span class=property>greeting</span><span class=punctuation>}</span><span class=string>, </span><span class=punctuation>${</span><span class=function>name</span><span class=punctuation>()}</span><span class=string>`</span><span class=punctuation>;</span>
  <span class=keyword>return</span> <span class=punctuation>(</span>
    <span class=operator>&lt;</span><span class=variable>form</span><span class=operator>></span>
      <span class=operator>&lt;</span><span class=constructor>Title</span> <span class=property>text</span><span class=operator>=</span><span class=punctuation>{</span><span class=function>title</span><span class=punctuation>()}</span> />
      <span class=operator>&lt;</span><span class=variable>input</span> <span class=property>type</span><span class=operator>=</span><span class=string>"text"</span> <span class=property>value</span><span class=operator>=</span><span class=punctuation>{</span><span class=function>name</span><span class=punctuation>()}</span> <span class=property>onChange</span><span class=operator>=</span><span class=punctuation>{</span><span class=variable>e</span> <span class=operator>=></span> <span class=function>setName</span><span class=punctuation>(</span><span class=variable>e</span><span class=punctuation>.</span><span class=property>target</span><span class=punctuation>.</span><span class=property>value</span><span class=punctuation>)}</span> />
    &lt;/<span class=variable>form</span><span class=operator>></span>
  <span class=punctuation>);</span>
<span class=punctuation>}</span></code></pre><p>When <code>title()</code> is called, it will then call both <code>name()</code> (obviously) and <code>props.greeting</code> (non-obviously, since it's a special getter, which is why <a href=https://docs.solidjs.com/concepts/components/props#destructuring-props>you can't destructure <code>props</code></a>). By calling these functions, anything that calls <code>title()</code> will then know to re-render whenever either <code>name()</code> or <code>props.greeting</code> changes. So, <code>&lt;Title /></code> will re-render when <code>props.greeting</code> changes, and both <code>&lt;Title /></code> and <code>&lt;input></code> will re-render when <code>name()</code> changes.</p><p>However, if you <em>don't</em> make <code>title</code> a function, then <code>name()</code> and <code>props.greeting</code> will be called "too early", and <code>&lt;Title /></code> will never re-render; the body of <code>Component</code> isn't hooked up to any reactivity, just special places like the return value & <code>createEffect</code>.</p><p>This whole approach makes fine-grained reactivity quite simple, but also makes actually writing components a bit complex. You have to hook up all the reactive parts yourself!! It's very easy to get this wrong (I did many times), and the tooling (eslint) isn't helpful at detecting when it happens.</p><h2 id=heading-react-may-over-render-but-at-least-its-easier-to-conceptualize>React may over-render, but at least it's easier to conceptualize<a aria-label="Link to heading 'React may over-render, but at least it's easier to conceptualize'" data-heading-content="React may over-render, but at least it's easier to conceptualize" class=anchor href=#react-may-over-render-but-at-least-its-easier-to-conceptualize></a></h2><p>Here's the equivalent React example; it's just minor syntactical differences:</p><pre class=syntax-highlighting><code class=language-jsx><span class=keyword>export</span> <span class=keyword>const</span> <span class=constructor>Component</span> <span class=operator>=</span> <span class=punctuation>({</span> greeting <span class=punctuation>})</span> <span class=operator>=></span> <span class=punctuation>{</span>
  <span class=keyword>const</span> <span class=punctuation>[</span><span class=variable>name</span><span class=punctuation>,</span> <span class=variable>setName</span><span class=punctuation>]</span> <span class=operator>=</span> <span class=function>useState</span><span class=punctuation>(</span><span class=string>""</span><span class=punctuation>);</span>
  <span class=keyword>const</span> <span class=variable>title</span> <span class=operator>=</span> <span class=string>`</span><span class=punctuation>${</span><span class=variable>greeting</span><span class=punctuation>}</span><span class=string>, </span><span class=punctuation>${</span><span class=variable>name</span><span class=punctuation>}</span><span class=string>`</span><span class=punctuation>;</span>
  <span class=keyword>return</span> <span class=punctuation>(</span>
    <span class=operator>&lt;</span><span class=variable>form</span><span class=operator>></span>
      <span class=operator>&lt;</span><span class=constructor>Title</span> <span class=property>text</span><span class=operator>=</span><span class=punctuation>{</span><span class=variable>title</span><span class=punctuation>}</span> />
      <span class=operator>&lt;</span><span class=variable>input</span> <span class=property>type</span><span class=operator>=</span><span class=string>"text"</span> <span class=property>value</span><span class=operator>=</span><span class=punctuation>{</span><span class=variable>name</span><span class=punctuation>}</span> <span class=property>onChange</span><span class=operator>=</span><span class=punctuation>{</span><span class=variable>e</span> <span class=operator>=></span> <span class=function>setName</span><span class=punctuation>(</span><span class=variable>e</span><span class=punctuation>.</span><span class=property>target</span><span class=punctuation>.</span><span class=property>value</span><span class=punctuation>)}</span> />
    &lt;/<span class=variable>form</span><span class=operator>></span>
  <span class=punctuation>);</span>
<span class=punctuation>}</span></code></pre><p>How React works is, any time the component props/<code>useState</code> values change, it runs the component body again, as well as any children, recursively. This gets us a lot of neat benefits over SolidJS, like:</p><ul><li>you can destructure <code>props</code></li><li>not everything has to be functions</li><li>if something changes in state, it is guaranteed to cause a re-render that will display the change.</li></ul><p>That approach would be expensive if, when a re-render happened, we had to remove all the old HTML elements from the page and add back the new ones, so instead React uses something called "Virtual DOM Diffing" to only call browser APIs for elements/attributes that need changing. Some unavoidable downsides are the fact you need to keep a virtual copy of the whole tree in-memory, as well as the pretty complex algorithm to do the diffing.</p><p>SolidJS cuts thru all this implementation complexity & potential runtime wastefulness by composing a simple + powerful primitive, signals. Unfortunately, it's a bit <em>too</em> simple + powerful, ending up finnicky to use as a result, like a Lisp, perhaps.</p><p>If you're making a non-trivial Javascript-powered application, just use React. The <a href=https://react.dev/learn/react-compiler>React Compiler</a> takes care of a lot of the problems React has, effectively providing fine-grained reactivity "for free" (bit more compile-time cost oop). <a href=https://preactjs.com/>Preact</a> is also available if you care about bundle sizes; the only reason I didn't choose it initially was because I wanted to learn a new framework. Still, sometimes u just gotta go back to ol' reliable.</p>]]></content>
      <author>
        <name>PolyWolf</name>
        <uri>https://wolf.girl.technology/</uri>
      </author>
    </entry>
  
    <entry>
      <title><![CDATA[Optimizing Markdown Images With Astro]]></title>
      <link rel="alternate" href="https://wolfgirl.dev/blog/2025-02-12-optimizing-markdown-images-with-astro/" />
      <id>https://wolfgirl.dev/blog/2025-02-12-optimizing-markdown-images-with-astro/</id>
      <published>2025-02-13T03:36:35Z</published>
      <updated>2025-02-13T03:36:35Z</updated>
      <summary><![CDATA[I dug into this because it had been on my mind for a while, and I wanted to get something working before I uploaded Japan trip photos. A...]]></summary>
      <content type="html"><![CDATA[<p>A previous version of this post contained some factual errors. I'm re-publishing this under a new URL so that it will update in your RSS reader. Updates are noted where applicable.</p><hr><p>I dug into this because it had been on my mind for a while, and I wanted to get something working before I uploaded Japan trip photos. A couple nights later, I have learned a bit about how Astro & Remark/Rehype & Vite work, and a lot about how what I'm trying to do is futile.</p><p>From my understanding, this is how Astro optimizes <a href=https://docs.astro.build/en/guides/images/#images-in-markdown-files>local images in Markdown files</a>:</p><ol><li><a href=https://github.com/withastro/astro/blob/8d4e566f5420c8a5406e1e40e8bae1c1f87cbe37/packages/markdown/remark/src/remark-collect-images.ts>remark-collect-images.ts</a>, a Markdown AST transformation step, finds all the image paths that look like <code>![]()</code> and not <code>&lt;img src="" /></code>, so we only optimize the Markdown images. It records this information into context that's passed to the next step.</li><li><a href=https://github.com/withastro/astro/blob/8d4e566f5420c8a5406e1e40e8bae1c1f87cbe37/packages/markdown/remark/src/rehype-images.ts>rehype-images.ts</a>, an HTML AST transform step, turns all those images that look like <code>&lt;img src="./local.png" /></code><sup class=footnote-ref><a data-footnote-ref href=#fn-1 id=fnref-1>1</a></sup> and turns them into something that looks like <code>&lt;img __ASTRO_IMAGE_="{&amp;#x22;src&amp;#x22;:&amp;#x22;./local.png&amp;#x22;,&amp;#x22;index&amp;#x22;:0}" /></code>. It does this because it's easier to look for, and safer to parse later compared to raw HTML.</li><li><a href=https://github.com/withastro/astro/blob/8d4e566f5420c8a5406e1e40e8bae1c1f87cbe37/packages/astro/src/vite-plugin-markdown/images.ts>vite-plugin-markdown/images.ts</a> takes the list of local image URLs (generated as part of transforming the Markdown into HTML in the above steps), and the HTML with these <code>__ASTRO_IMAGE_</code> things in them, and generates a full-on Astro component, Javascript source & everything, to be rendered later.</li></ol><p>Phew, that's a lot!! Hope you're still following along :)</p><p>So, the procedure I described works perfectly fine for standalone <code>.md</code> pages. The problem comes when trying to apply this same transformation to markdown files rendered as part of a <a href=https://docs.astro.build/en/reference/modules/astro-content/>content collection</a>, which is what my blog files are stored as. Instead of being rendered as part of the "main" Astro flow, content collection entries are rendered separately, as part of a loader, like <a href=https://github.com/withastro/astro/blob/8d4e566f5420c8a5406e1e40e8bae1c1f87cbe37/packages/astro/src/content/loaders/glob.ts#L183>content/loaders/glob.ts</a>. For Markdown, this calls into <a href=https://github.com/withastro/astro/blob/8d4e566f5420c8a5406e1e40e8bae1c1f87cbe37/packages/astro/src/vite-plugin-markdown/content-entry-type.ts>vite-plugin-markdown/content-entry-type.ts</a>, which does no such image replacement, because content entries aren't in the correct stage of the build.</p><p><del>And I don't think we <em>can</em> get to the correct stage here! The whole reason this code does the <code>__ASTRO_IMAGE_</code> rigamarole is so it can call the image transformation function at the correct time. With content collections, all the rendering happens "too early", so the images can't be transformed.</del> UPDATE 2025-02-15: This is incorrect. The replacement happens in <a href=https://github.com/withastro/astro/blob/3842ce5ec9471d358042b3d9ef697cf06c7a91f6/packages/astro/src/content/runtime.ts#L401-L447>content/runtime.ts</a>, during the actual rendering, which makes sense. I wasn't seeing this before because (a) im bad at looking and (b) it's gated on Astro detecting images in the body, which it does not currently do for remote images.</p><p>Alternatively, I <em>could</em> use <a href=https://docs.astro.build/en/guides/integrations-guide/mdx/>MDX</a>, which is like Markdown but compiles to Javascript instead of HTML, which <em>does</em> always have the image transformation step, thanks to <a href=https://github.com/withastro/astro/blob/8d4e566f5420c8a5406e1e40e8bae1c1f87cbe37/packages/integrations/mdx/src/rehype-images-to-component.ts>rehype-image-to-component.ts</a>. However, I do not like MDX's weird character restrictions (c'mon, no <code>&lt;</code>, really??), so I will not use it >:(</p><p>At this point, I'm seriously considering moving away from Astro's built-in image optimization service to my own... For now though, I'm giving up 😔</p><p>UPDATE 2025-02-13: I have just discovered that <a href=https://docs.astro.build/en/guides/integrations-guide/markdoc/#custom-image-components>Markdoc</a> does exactly what I want it to: provide the ability to use a custom component to override what <code>![]()</code> means, without limiting Markdown syntax. This is very very good news! Here's hoping those files will still have reasonable build times. Also I should probably make it so my Post composer re-uses the Markdoc config...</p><p>UPDATE 2024-02-14: Markdoc also doesn't work for my needs because it doesn't have footnote support :((((( I make heavy use of footnotes in my text and I do not want to change how I write those. The whole <em>point</em> of Markdown is to make it so you don't look like you're formatting your text with tags, and Markdoc ignores that and immediately goes back to demanding you use tags for nonstandard features. It seems I am better served by the remark/rehype ecosystem for my parsing needs, so I will continue to do that.</p><p>UPDATE 2024-02-15: I did it! I fixed Astro!! The above information was a tad incorrect, what I wanted to do was possible with a bit of elbow grease, so I put in the work and opened a couple PRs: <a href=https://github.com/withastro/astro/pull/13254>#13254</a> & <a href=https://github.com/withastro/astro/pull/13256>#13256</a>. I also have the necessary patches for <a href=https://github.com/p0lyw0lf/website/blob/d7a866fef2356fb194527fc7cf48c8f20e616cc6/patches/astro.patch>astro</a> and <a href=https://github.com/p0lyw0lf/website/blob/d7a866fef2356fb194527fc7cf48c8f20e616cc6/patches/%40astrojs__markdown-remark.patch>@astrojs/markdown-remark</a> in my website directly, so I don't have to wait for them to be upstreamed.</p><section class=footnotes data-footnotes><ol><li id=fn-1><p>I believe this is a minor bug: if you have both <code>![](./local.png)</code> <em>and</em> <code>&lt;img src="./local.png" /></code>, I think the latter image will be transformed when it shouldn't be. At least, that's if I'm reading the code right, which I may well not be :P <a aria-label="Back to reference 1" class=footnote-backref data-footnote-backref data-footnote-backref-idx=1 href=#fnref-1>↩</a></p></li></ol></section>]]></content>
      <author>
        <name>PolyWolf</name>
        <uri>https://wolf.girl.technology/</uri>
      </author>
    </entry>
  
    <entry>
      <title><![CDATA[Some Reasons Your React App Might Be Slow]]></title>
      <link rel="alternate" href="https://wolfgirl.dev/blog/5801824/" />
      <id>https://wolfgirl.dev/blog/5801824/</id>
      <published>2024-05-02T01:29:01Z</published>
      <updated>2024-05-02T01:29:01Z</updated>
      
      <content type="html"><![CDATA[<p>Originally published on Cohost in 2 parts.</p><hr><p><a href=https://mastodon.social/@danluu/112368308357914085>Dan Luu, on Mastodon</a>:</p><blockquote><p>Naive question: why do React apps in the real world tend to be slow?</p><p>I tried doing a React tutorial and the result was quite fast (w.r.t. latency & CPU utilization on low-end devices) until the tutorial has you replace "manual" / "low-level" react calls with commonly used libraries, e.g., using TanStack Query instead of useEffect plus a manually instantiated cache.</p><p>Is the main issue that libraries tend to be big and slow or is there another major cause of React app slowness?</p></blockquote><p>wow, a question i actually have a smidgen of expertise on!</p><p>At work, we have a very big React app. Here are some things which were causing slowness, which we know for certain because fixing them improved performance:</p><ol><li>Excessive use of react-redux and redux-saga. These are libraries which have you put everything you want to share between unrelated (not direct children) components in global state, and then runs "selectors" (arbitrary javascript functions to get specific data out of the global state) every time any of that state changes. Across thousands of selectors and many state changes per second, this really adds up. We have good reason to believe we're one of the largest users of Redux, so this might not be an issue for everyone.</li><li>Excessive rerenders, and non-memoized rerenders. You can have excessive rerenders when your component gets non-memoized objects (or, functions, arrays, etc.) as props. A child can do all the proper useCallback and useMemo it wants, but it's on the parent to not mess up. Which happens often since it's a pain to wrap every single arrow function in useCallback everywhere (React docs explicitly say to not do this until performance is a concern, even!). And even then, if a parent rerenders for whatever reason, it will rerender all its children, even if their props are all the same, unless the child is memoized, which is its own problem if the heirarchy is very deep.</li><li>Too Many DOM nodes. This is a general problem with component-like frameworks, encouraging a kind of abstraction leading to far too many wrapper divs since they're so easy to add. All these extra DOM nodes come at a surprising performance penalty, especially in browsers like Safari.</li></ol><p>Not quite an exhaustive "things that make your React app slow", but hopefully useful nonetheless :P</p><hr><h1 id=heading-other-reasons-your-very-big-website-might-be-very-slow>Other Reasons Your Very Big Website Might Be Very Slow:<a aria-label="Link to heading 'Other Reasons Your Very Big Website Might Be Very Slow:'" data-heading-content="Other Reasons Your Very Big Website Might Be Very Slow:" class=anchor href=#other-reasons-your-very-big-website-might-be-very-slow></a></h1><ol start=4><li>Animations. The designer loves to go ham and make absolutely everything animated, but eventually yeah those start to add up too, especially when animating massive DOM trees and causing big layout shifts. Combined with (3) above, it sometimes takes a solid <strong>2 seconds</strong> to open a certain problematic side panel, on a good machine. Good luck on a little dinky machine. This problem is also exacerbated by using libraries like framer-motion, which have lots of cool animation curves, not all of them implemented efficiently! Using CSS animations where possible also lets you quickly turn them off with <code>animation: none !important; transition: none !important;</code></li><li>Opacity. The designer also loves using <code>rgba(255, 255, 255, 0.05)</code> for component backgrounds, because it layers nicely over almost any other background. Unfortunately, they require more work by the browser to render properly, which can amount to a surprising drop in performance on weaker clients. Especially when combined with animated transparencies🙀 Making an option to change to all flat colors was a noticeable gain. And this is all to say nothing of blur, which tanks performance even harder (we've almost got rid of it entirely now)</li><li>Excessive Repaints From Weird Layouts. Still haven't quite gotten to the bottom of this so can't say for sure, but it seems that certain layout things (CSS grid? Weird stacking contexts?) cause browsers to think they need to repaint more than what's actually changed, leading to a drop in performance. Saying "hey change the size of this one thing slightly" makes it go "oh i should repaint the whole screen, got it", even when there's no actual layout shift. This is once again exacerbated by animations, which cause this many times a second.</li></ol><p>Once again, these are just problems we've identified, and there may be more, I am by no means a Web Performance Expert. yet.</p>]]></content>
      <author>
        <name>PolyWolf</name>
        <uri>https://wolf.girl.technology/</uri>
      </author>
    </entry>
  
    </feed>
  