การเพิ่มประสิทธิภาพของ Vue ครอบคลุมขนาดแพคเกจ ประสิทธิภาพการเรนเดอร์ และต้นทุนของความเป็นปฏิกิริยา นี่คือเทคนิคที่มีผลกระทบสูงสุด
1. การแบ่งโค้ด / การโหลดแบบเกียจคร้าน
routes = [{ : , : () }];
= ( ());
การเพิ่มประสิทธิภาพของ Vue ครอบคลุมขนาดแพคเกจ ประสิทธิภาพการเรนเดอร์ และต้นทุนของความเป็นปฏิกิริยา นี่คือเทคนิคที่มีผลกระทบสูงสุด
routes = [{ : , : () }];
= ( ());
ชัยชนะการโหลดเริ่มต้นที่ใหญ่ที่สุด: อย่าส่งโค้ดที่ผู้ใช้ไม่จำเป็นต้องใช้ในตอนนี้
<div v-show="tab === 'a'"> <!-- toggled often → cheap CSS flip -->
<div v-if="rarelyShown"> <!-- rarely rendered → skip building it -->
<li v-for="item in items" :key="item.id"> <!-- enables efficient DOM reuse/diffing -->
import { shallowRef, shallowReactive } from "vue";
// for large objects you replace wholesale (or 3rd-party instances), skip deep tracking
const bigData = shallowRef(hugeObject); // only .value reassignment is reactive
Object.freeze(staticConfig); // never-changing data → no reactivity overhead
ความเป็นปฏิกิริยาเชิงลึกบนวัตถุขนาดใหญ่มีราคาแพง; shallowRef/shallowReactive/Object.freeze หลีกเลี่ยงการติดตามข้อมูลที่ไม่จำเป็น
<p>{{ expensiveComputed }}</p> <!-- cached, recomputes only on dependency change -->
<header v-once>{{ siteName }}</header> <!-- render once, never update -->
<div v-memo="[item.id]">...</div> <!-- re-render only if item.id changes -->
Rendering 10,000 rows kills performance → use vue-virtual-scroller / TanStack Virtual
to render only the visible rows.
Vue DevTools (component render timings, why a component re-rendered)
build output / rollup-plugin-visualizer → bundle composition
Lighthouse → Core Web Vitals
Vue มีความเร็วตามค่าเริ่มต้น แต่แอปขนาดใหญ่ต้องการการเพิ่มประสิทธิภาพโดยตั้งใจ: lazy-load เพื่อลดขนาดแพคเกจ เลือก v-if/v-show อย่างถูกต้อง ใช้คีย์ที่เสถียร หลีกเลี่ยงความเป็นปฏิกิริยาที่มากเกินไปบนข้อมูลขนาดใหญ่ (shallowRef/freeze) แคชด้วย computed/v-memo และเรนเดอร์เสมือนรายการยาว
การรู้ว่าเทคนิคใดแก้ไขคอขวดใด — และวัดด้วย DevTools/Lighthouse แทนที่จะเดา — คือสิ่งที่ทำให้แอป Vue ที่กำลังเติบโตยังคงตอบสนองได้
คลังคำถามสัมภาษณ์งาน IT พร้อมคำตอบโดยละเอียด — ตั้งแต่ระดับ Junior ถึง Senior
บริจาค