Boost Vue App Speed: Proven Frontend Optimization Techniques
This article walks through practical Vue performance enhancements—including basic web rendering tweaks, webpack configuration tricks, Vue‑specific code optimizations, and auxiliary measures like GZIP compression, HTTP caching, CDN usage, image handling, and lazy loading—to dramatically improve first‑screen load times and overall user experience.
Vue handles many heavy tasks such as two‑way data binding and Virtual DOM, allowing developers to focus on business logic, but first‑screen rendering, webpack configuration, resource loading speed, and runtime performance still need attention.
Optimization Modules
Basic web technology optimization
Webpack bundling configuration optimization
Vue code‑level optimization
Other optimizations
From URL Input to Page Load
The request flow consists of client request, server response, and client rendering, highlighting that front‑end improvements target the first and third stages.
Basic Web Technology Optimization
Page Rendering Performance
Browser renders a page through the following steps:
DOMTree: parse HTML to build the DOM tree.
CSSOMTree: parse CSS to build the CSSOM rule tree.
RenderObjectTree: merge DOM and CSSOM into a render object tree.
Layout: calculate size and position for each node.
Painting: draw each node onto the screen.
Practical measures:
1. Prevent Render‑Blocking
Place CSS in the <head> to load early.
Put JS files at the bottom of <body>.
Use defer or async for non‑critical JS.
2. Reduce Repaint and Reflow
Avoid frequent DOM and CSS property access; prefer CSS solutions.
Use position: fixed or absolute for animated elements.
Specify width and height on <img> tags.
Prefer transform over left/top for movements.
3. Reduce DOM and CSSOM Build Time
Keep DOM depth shallow.
Minimize CSS nesting and use efficient selectors.
Server‑Side Support
GZIP Compression
Enable GZIP in the server to compress responses (≈70% reduction). Vue 2.0 with webpack can use compression-webpack-plugin:
if (config.build.productionGzip) {
const CompressionWebpackPlugin = require('compression-webpack-plugin')
webpackConfig.plugins.push(
new CompressionWebpackPlugin({
asset: '[path].gz[query]',
algorithm: 'gzip',
test: new RegExp('\\.(' + config.build.productionGzipExtensions.join('|') + ')$'),
threshold: 10240,
minRatio: 0.8
})
)
}Activate the GZIP switch in index.js and ensure the server supports it.
HTTP Caching
Cache static resources via response headers to avoid repeated network requests.
CDN Usage
Load CSS, JS, and images from CDN domains to increase concurrent connections, reduce latency, and improve availability.
Chrome Performance Tool
Open DevTools → Performance panel.
Click Record, refresh or interact, then Stop.
Webpack Bundling Optimization
webpack‑bundle‑analyzer
Install and configure to visualize bundle size:
npm i webpack-bundle-analyzer -D const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
new BundleAnalyzerPlugin({
analyzerMode: 'server',
analyzerHost: '127.0.0.1',
analyzerPort: 8889,
reportFilename: 'report.html',
defaultSizes: 'parsed',
openAnalyzer: true,
generateStatsFile: false,
statsFilename: 'stats.json',
statsOptions: null,
logLevel: 'info'
})Run npm run build to generate the report.
On‑Demand Import of Third‑Party Plugins
For element‑ui, install babel-plugin-component and configure .babelrc to import only needed components, reducing the vendor bundle from 553 KB to 267 KB.
npm install babel-plugin-component -D {
"presets": [["env", {"modules": false, "targets": {"browsers": ["> 1%", "last 2 versions", "not ie <= 8"]}}], "stage-2"],
"plugins": [["component", {"libraryName": "element-ui", "styleLibraryName": "theme-chalk"}]]
}CDN External Loading
Declare external libraries in webpack.base.conf.js and reference them via CDN URLs in index.html:
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/axios.min.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>Image Compression
Install image-webpack-loader and configure in webpack.base.conf.js to compress PNG, JPEG, GIF, etc.
npm install image-webpack-loader -D module: {
rules: [{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
use: [{ loader: 'url-loader', options: { limit: 10000, name: utils.assetsPath('img/[name].[hash:7].[ext]') } },
{ loader: 'image-webpack-loader', options: { mozjpeg: { progressive: true, quality: 65 }, optipng: { enabled: false }, pngquant: { quality: [0.65,0.90], speed: 4 }, gifsicle: { interlaced: false } } }]
}]
}Vue Code‑Level Optimization
Route Lazy Loading
{
path: '/index',
name: '首頁',
component: r => require.ensure([], () => r(require('@/page/index')), 'index'),
meta: { type: 'index', title: 'XXX' }
}v‑if vs v‑show
Use v-show for frequently toggled elements and v-if for rarely changed conditions.
Long/Infinite List Rendering
Apply windowed rendering with vue-virtual-scroll-list or vue-virtual-scroller to render only visible items.
Conditional Statement Optimization
Prefer early returns to avoid deep if‑else nesting and use Map for fast lookups.
if (res.code === -1) return false;
// ...other logic let map = new Map();
for (let i = 0; i < s.length; i++) {
if (!map.has(s[i])) {
a++;
} else {
let temp = map.get(s[i]);
a = temp > a ? temp : a;
b++;
}
map.set(s[i], i);
}Other Optimizations
Image Compression Tools
Use services like Panda compression; prefer JPEG over PNG for smaller size.
Image Preloading
Preload critical images via JavaScript or Preload.js to display loading progress.
let imgs = ["static/img/card1.png", "static/img/card2.png", /* ... */];
imgs.forEach(src => { let img = new Image(); img.src = src; img.onload = () => { /* update progress */ }; });Image Lazy Loading
Install vue-lazyload and replace :src with v-lazy in templates.
npm install vue-lazyload
Vue.use(VueLazyload, { preLoad: 1.3, error: 'dist/error.png', loading: 'dist/loading.gif', attempt: 1 }) <img v-lazy="item.src">Network Request Optimization
Defer or async load non‑critical scripts (e.g., Facebook SDK).
Combine small assets, use CSS sprites.
let fbDiv = document.createElement('script');
fbDiv.setAttribute('async', 'async');
fbDiv.setAttribute('defer', 'defer');
fbDiv.setAttribute('crossorigin', 'anonymous');
fbDiv.setAttribute('src', 'https://connect.facebook.net/zh_TW/sdk.js#xfbml=1&version=v6.0&appId=xxxx');
document.body.appendChild(fbDiv);Google Analytics and other analytics scripts should be placed in index.html with the standard snippet.
In summary, the article covers four parts—basic web‑level optimization, webpack configuration tweaks, Vue code‑level improvements, and miscellaneous techniques—to help developers enhance Vue project performance.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
