Does parsed data improve speed

Hello, let's say I parse data this way. Does it make my website faster / lighter when I use this query in the frontend, because I return just the stuff I want?
const PokemonResult = z.object({
forms: z.array(z.object({
name: z.string(),
url: z.string(),
})),
base_experience: z.number(),
})
const PokemonResult = z.object({
forms: z.array(z.object({
name: z.string(),
url: z.string(),
})),
base_experience: z.number(),
})

And then create a router
export const pokeRouter = router({
getByName: publicProcedure
.input(z.object({ name: z.string()}))
.query(async ({ input }) => {
const pokemon = await fetch(`https://pokeapi.co/api/v2/pokemon/${input.name}`).then(res =>
res.json());
const parsedData = PokemonResult.parse(pokemon);

return parsedData;
}),
})
export const pokeRouter = router({
getByName: publicProcedure
.input(z.object({ name: z.string()}))
.query(async ({ input }) => {
const pokemon = await fetch(`https://pokeapi.co/api/v2/pokemon/${input.name}`).then(res =>
res.json());
const parsedData = PokemonResult.parse(pokemon);

return parsedData;
}),
})
2 Replies
tom0619
tom06193y ago
The response from the network request will be slightly smaller, so you might save a couple of kb. I wouldn't do this just to save some data there. However, validating the output, specially when the data comes from an external source is generally a good idea. https://twitter.com/alexdotjs/status/1589007455295397890 You can also validate the output by using .output and putting your schema in there: https://trpc.io/docs/v9/output-validation
noctate
noctateOP3y ago
Thx! Love your videos btw
Want results from more Discord servers?
Add your server