Comments (2)
TL;DR: Declaring SatoriOptions.fonts as a global variable doubled the performance
Upon closer inspection of the code, I discovered a global variable named fontCache
that utilizes WeakMap. I added console.log
to satori.ts#L57-L62
to check if the cache was indeed being utilized.
let font: FontLoader
if (fontCache.has(options.fonts)) {
font = fontCache.get(options.fonts)
} else {
fontCache.set(options.fonts, (font = new FontLoader(options.fonts)))
}
It turned out, in satori.ts, the cache key was being assigned to the memory address of the SatoriOptions.fonts
object.
We had been declaring SatoriOptions.fonts as a local variable, just as the example in README.md suggests.
This made me realize that changing the address of options.fonts with every invocation prevented us from ever utilizing the cache effectively, as it just kept accumulating.
Therefore, I declared SatoriOptions.fonts as a global variable as follows:
AS-IS
export default async function handler(
request: NextApiRequest,
response: NextApiResponse
) {
try {
const svg = await satori(JSX,
{
width: 1200,
height: 630,
embedFont: true,
fonts: [
// ...
]
);
TO-BE
const options: SatoriOptions = {
width: 1200,
height: 630,
embedFont: true,
fonts: [
// ...
],
};
export default async function handler(
request: NextApiRequest,
response: NextApiResponse
) {
try {
const svg = await satori(JSX, options);
// ...
As a result, the same code was able to handle approximately double the number of requests.
wrk -t10 -c 30 -d30s http://localhost:3000/og/review/41854\?updatedAt\=2024-01-29T14:30:22
Running 30s test @ http://localhost:3000/og/review/41854?updatedAt=2024-01-29T14:30:22
10 threads and 30 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 992.55ms 273.04ms 1.87s 66.67%
Req/Sec 4.43 3.97 20.00 70.93%
891 requests in 30.07s, 318.15MB read
Requests/sec: 29.63
Transfer/sec: 10.58MB
- AS-IS: 15.95 Requests/sec
- TO-BE: 29.63 Requests/sec
Performance improvement by roughly 1.86 times
As you can see, this changes no longer make unnecessary calls to addFonts() in the profiling graph.
Now, we can see that the sendData() function, used when returning a response, occupies most of the flame graph.
Required
- Modify
satori.ts#L57-L62
to use serialized value as key of the map instead of the memory address value of the object - ImageResponse provided by @next/og might also have similar issues.
I hope this helps those experiencing performance issues. Thank you.
Regarding this, I'd like to inform @shuding , the author of this code, about the matter. If it's not too much trouble, may I go ahead and make the improvements, then submit them as a Pull Request? @shuding
from satori.
Related Issues (20)
- Mask image always applied to root instead of element HOT 1
- TextDecoration issue when used with transform property
- `lucide-react` not rendering
- `filter: "blur(...)"` doesn't work as expected with Satori + resvg-js HOT 2
- Image mask causing image to be hidden behind other SVG elements HOT 2
- CDN image not rendering
- Support `font-feature-settings`
- Custom fonts not rendered correctly
- Support -webkit-text-stroke HOT 1
- Option to keep font-family case-sensitive
- Custom Font files not loaded on Vercel Edge Function
- Error with linear-gradient HOT 2
- @vercel/og will not work on edge outside of next - noto-sans-v27-latin-regular.ttf HOT 4
- Using CSS `border`, images are broken and exposed
- Optional TailwindCSS
- Functionality for getting bounding box of text from satori
- Title HOT 1
- Image in SVG doesn't work when exported to PNG HOT 10
- Transparent linear gradient rendered incorrectly HOT 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 satori.