Git Product home page Git Product logo

Comments (10)

mcollina avatar mcollina commented on May 27, 2024 2

I don't think this is really useful, because generators are all synchronous anyway, and concatenating strings is incredibly cheap. I would expect generators to worsen performance.

However, supporting promises and streaming the chunk after each one resolves, would boost time to first byte.

from fastify-html.

mcollina avatar mcollina commented on May 27, 2024 1

Do you think the TTFB would be slower in the following scenarios?

No, TTFB would exactly the same or slower, because all the data is going to the kernel in the same event loop tick. It can be slower because streams adds overhead and you already have all the data.

from fastify-html.

gurgunday avatar gurgunday commented on May 27, 2024

However, supporting promises and streaming the chunk after each one resolves, would boost time to first byte.

Alright, I will look into that, thanks for the input

Perf will definitely degrade I think, here the goal is faster TTFB

from fastify-html.

gurgunday avatar gurgunday commented on May 27, 2024

Do you think the TTFB would be slower in the following scenarios? Even though the final result might take longer, the important thing here is that the user will start seeing things much faster, no?

app.get("/", function (req, res) {
  const htmlContent = htmlGenerator`<html>
    <p>${"...your HTML content..."}</p>
  </html>`;
  const readableStream = Readable.from(htmlContent);
  res.send(readableStream);
});

Or (of course will be adapted for Fastify):

import { htmlGenerator as html } from "ghtml";
import http from "http";

http.createServer(async (req, res) => {
  if (req.url === '/stream') {
    res.writeHead(200, { 'Content-Type': 'text/html' });

  const htmlContent = html`<html>
  <p>${"...your HTML content..."}</p>
</html>`;
    for await (const chunk of htmlContent) {
      res.write(chunk);
    }

    res.end();
  } else {
    res.writeHead(404);
    res.end('Not Found');
  }
}).listen(3000, () => console.log('Server running on http://localhost:3000'));

from fastify-html.

gurgunday avatar gurgunday commented on May 27, 2024

https://frontendmasters.com/blog/streaming-html/

from fastify-html.

gurgunday avatar gurgunday commented on May 27, 2024

Yeah I just watched your masterclass, and to say the least, it came out at the perfect time 😄

So what I had in my mind with htmlGenerator won't improve TTFB, maybe I might add an async generator to combine with readFile, etc, but in any case thanks again for the information and the video 🙏

Closing for now

from fastify-html.

gurgunday avatar gurgunday commented on May 27, 2024

Hi! Quick question:

I updated the README over at ghtml to reflect on what I've learned, would you say this explanation is accurate?

from fastify-html.

mcollina avatar mcollina commented on May 27, 2024

It's slightly incorrect. If you have all the data, processing and rendering synchronously will be faster.

from fastify-html.

gurgunday avatar gurgunday commented on May 27, 2024

Yes, it says streaming with sync generators are never faster than processing directly without the generator

I'll try to improve the wording

from fastify-html.

gurgunday avatar gurgunday commented on May 27, 2024

Done: https://github.com/gurgunday/ghtml?tab=readme-ov-file#htmlgenerator

from fastify-html.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.