RedditPP Mascot

RedditPP

r/sveltejs • … • u/Rocket_Scientist2

Super-helpful deduplication pattern for transport hook (Gist link in post)

I was gonna write a blog post for this, but I didn't think it was worth it. I *really* wanted to share it though, so I'm directly posting here (on the subreddit)

**Background**

Recently I've been using [transport hooks](https://svelte.dev/docs/kit/@sveltejs-kit#Transport). It's *such* a good feature. Not only can they be used for sending data-classes over the wire, but you can use them with built-ins, such as `Error` or `URL`. Being able to return classes in load functions is beyond convenient. However, I ran into an issue.

**Example**

Let's say I have two classes: `App` and `User`. There are multiple `User`s per `App`, and each `User` holds a reference to its respective `App`. If I run a function to return 100 `User`s for an `App`, I will have:

* 1x `App` * 100x `User`

Now, if I go return my `User[]` in a load function, I will end up with:

* 100x `App` * 100x `Users`

*Whoops!*

In my case, `App` is **huge** when compared to `User`. On a small scale, it's fine. But when scaled up, this led to a *massive* slowdown, and exceeded memory limits when deployed to Cloudflare Pages.

**Why?**

JSON has no concept of deduping. It will simply follow down the tree, until everything has been `stringify()`-ed or it throws (this can happen if two objects hold references to each other). This means that every reference to an `App`:

* Serializes it again (on encode) * Spawns a new `App` (on decode)

**How To Fix?**

Well, obviously you could just abstain from holding references onto other classes, but that sucks.

A cooler option would be to memoize our `encode`\-ing and `decode`\-ing processes. As it happens, we can implement this for `App` only, to solve our issue. This is because transports work on nested objects; anything that uses `App` can take advantage of this!

Using this method, I was able to bring my load times down from "20 seconds then memory error" to "under 1 second".

[Here's a super small gist I wrote](https://gist.github.com/colecrouter/f2660efcfb073dc35f6b439692b0beee) showing how you could adopt a similar pattern in your own codebase. I'd love to hear your thoughts about this, and if/how anyone else has been using the `transport` hook.

— 0