Initializing, please wait a moment

Cach minify CSS / JS de gianh cold-start tren Cloud Run


Thoi gian cold-start tren Google Cloud Run, AWS Lambda, Cloudflare Workers va cac nen tang serverless tuong tu bi chi phoi boi hai dieu: chi phi mang de gui code cua ban den runtime, va thoi gian runtime danh ra de parse. Minify CSS va JavaScript truc tiep giam ca hai. Voi mot dich vu Cloud Run dien hinh huong toi web, cat 40% bundle JS tuong duong ~100 ms it hon tren duong cold-start. Huong dan nay bao quat cac cong thuc build-step chinh xac hoat dong tren cac runtime chinh, voi cac con so truoc/sau duoc do dac.


Tai sao minify dac biet giup cold-start

Truyen tai mang. Cac nen tang serverless keo container image hoac bundle den runtime khi cold start. Bundle nho hon → pull nhanh hon. Tren Cloud Run, pull container image layer co the la dong gop lon nhat cho cold-start tren mot dich vu nho (~300 ms cho image 50 MB). Cloudflare Workers co cap 1 MB o free tier va 10 MB o paid - minify thuong la su khac biet giua vua va khong vua.

Thoi gian parse. V8 (Node / Workers / Deno) parse JavaScript mot lan moi isolate. JS minified co it token hon, it khoang trang hon, va id ngan hon - V8 parse nhanh hon ~30%. Parse la mot thao tac dong bo chan request dau tien; tren cold start no la su khac biet giua 120 ms va 160 ms tren bundle 500 KB.

Hieu qua nen. JS minified nen tot hon duoi gzip/brotli vi khoang trang lap lai va comment khong tieu thu tu dien cua bo nen. Tiet kiem ban toi muc ~15-25% ngoai unminified+gzip.


Minify truc tuyen cho kiem tra mot lan

Cho mot tep duy nhat hoac mot phep do truoc/sau nhanh, dan vao cac cong cu trinh duyet cua chung toi:

Cac cong cu nay chay trong trinh duyet - khong gi tai len. Huu ich de do mot tep cu the hoac kiem tra hinh thuc nen cua SDK ben thu ba.


Minify trong buoc build (cau tra loi production thuc su)

Voi dich vu thuc te, minify la mot build-step, khong phai mot lan dan. Ba cau hinh thong dung:

esbuild (nhanh nhat; mac dinh cho hau het stack hien dai).

npx esbuild src/index.ts \
  --bundle \
  --minify \
  --target=node20 \
  --platform=node \
  --outfile=dist/index.js

Flag --minify cua esbuild thuc hien doi ten identifier, loai bo dead-code, va xoa khoang trang. Them --tree-shaking=true (mac dinh khi bundle) de drop cac export khong duoc tham chieu. Chi minify (khong bundle) qua npx esbuild in.js --minify > out.js.

Terser (truong thanh; tao ra dau ra JS nho nhat).

npx terser dist/index.js \
  -o dist/index.min.js \
  -c passes=3,pure_getters,unsafe_arrows=true \
  -m toplevel=true \
  --source-map content=inline,url=dist/index.min.js.map

Ba lan nen + top-level mangling + unsafe_arrows (chuyen bieu thuc ham co ten thanh arrow function khi tuong duong) co the cat them 5-10% so voi esbuild. Cham hon de chay - dung tren build production cuoi cung thoi.

cssnano / lightningcss cho CSS.

npx lightningcss \
  --minify \
  --targets ">= 0.25%" \
  src/app.css -o dist/app.css

lightningcss ket hop transpile (autoprefixer) va minify trong mot buoc; dua tren Rust, nhanh hon ~20× so voi postcss+cssnano.


Flag rieng cho tung runtime

Cloud Run (dua tren container). Don bay chinh la kich thuoc container image. Dung Dockerfile multi-stage: build bundle trong stage builder Node, sau do copy dau ra minified vao stage runtime distroless. Bo qua source map trong production. Nham toi image 20-50 MB.

AWS Lambda. Lambda layer gui unminified de debuggable, handler gui minified. Gioi han 50 MB zipped / 250 MB unzipped hiem khi cham tren dich vu JS, nhung chi phi parse cold-start van duoc loi tu minify.

Cloudflare Workers. Cap bundle 1 MB (free) / 10 MB (paid) thuong buoc minify. Workers dung V8 isolate; minify cat ca buoc transfer va parse. Dung config esbuild built-in cua wrangler: wrangler deploy --minify.

Deno Deploy / Vercel Edge. Tuong tu Cloudflare Workers - V8 isolate, nhay cam voi kich thuoc bundle. Ca hai chay esbuild noi bo voi --minify mac dinh.


Tac dong do dac - dich vu web dien hinh

Mot dich vu Cloud Run mau (Node 20, Express, 300 KB bundle JS unminified, 50 KB CSS) truoc/sau minify:

Chi soUnminifiedMinifiedDelta
Kich thuoc bundle JS300 KB160 KB-47%
JS da gzip95 KB65 KB-32%
Cold-start (p50)680 ms580 ms-100 ms
Cold-start (p95)1200 ms990 ms-210 ms
Request am (p50)18 ms17 ms-1 ms (nhieu)

Minify giup cold-start khong can xung - request am khong re-parse bundle, nen tiet kiem tap trung vao cac lan goi lanh. Voi dich vu thay luu luong on dinh, loi cold-start tinh te; voi dich vu dot bien hoac luu luong thap, loi co y nghia.


Nhung gi KHONG nen lam

Khong gui unminified va dua vao gzip. Gzip giup nhung khong cham vao thoi gian parse. Minify truoc, sau do nen.

Khong minify script inline trong HTML khi runtime. Minify khi build. Minify khi runtime dot CPU tren moi request.

Khong xoa source map trong development. Stack trace loi tro nen kho doc. Gui map nhu cac tep .map rieng, loai khoi bundle, phuc vu voi header HTTP SourceMap chi khi duoc yeu cau.

Khong dung flag unsafe minifier tren ma SDK ben thu ba. Mot so SDK (Sentry, Stripe.js) dua vao introspection ten ham cu the; mangling manh tay lam vo chung. Giu bundle vendor khong mangle.


Cong cu lien quan


← Quay lai Developer Tools

Why trust these tools

  • Ten-plus years of web tooling. The freetoolonline editorial team has shipped browser-based utilities since 2015. The goal has never changed: get you to a working output fast, without an install.
  • Truly in-browser - no upload. Every file-processing tool on this site runs in your browser through modern Web APIs (File, FileReader, Canvas, Web Audio, WebGL, Web Workers). Your photo, PDF, audio, or text never leaves your device.
  • No tracking during tool use. Analytics ends at the page view. The actual input you paste, drop, or capture is never sent to any server and never written to any log.
  • Open-source core components. The processing engines underneath (libheif, libde265, pdf-lib, terser, clean-css, ffmpeg.wasm, and others) are public and audit-able. We link to each one in its tool page's footer.
  • Free, with or without ads. All tools are fully functional without sign-up. The Disable Ads button in the header is always available if you need a distraction-free run.