Comments (10)
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.
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.
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.
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.
https://frontendmasters.com/blog/streaming-html/
from fastify-html.
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.
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.
It's slightly incorrect. If you have all the data, processing and rendering synchronously will be faster.
from fastify-html.
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.
Done: https://github.com/gurgunday/ghtml?tab=readme-ov-file#htmlgenerator
from fastify-html.
Related Issues (2)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastify-html.