← Back to React Course | Chapter 14: Performance & Production Deployment | Lesson 5 of 13

Bundle Size का विश्लेषण

अपना bundle size analyze करना एक flight से पहले अपना suitcase weigh करने जैसा है — आप exactly देखना चाहते हैं क्या space ले रहा है ताकि आप decide कर सकें actually क्या pack करना worth है।

Bundle Size क्यों मायने रखता है

एक बड़ा JavaScript bundle आपके app के interactive होने से पहले download और parse होने में ज़्यादा समय लेता है, directly load performance को hurt करते हुए, खासकर slower connections या devices पर।

यह समझना कि actually आपके bundle के अंदर क्या है इसे वहां trim करने की तरफ पहला step है जहां मायने रखता है।

Note: Bundle size सबसे ज़्यादा उस INITIAL bundle के लिए मायने रखता है जो एक user कुछ भी render होने से पहले download करता है — code-splitting (React.lazy) less-critical code को उस initial payload से बाहर move कर सकता है।
Warning: हर बड़ी dependency हटाने के लिए worth नहीं है — actual size cost को इसके द्वारा दी जाने वाली real functionality/development-time value के against weigh करें।

उदाहरण: Why Bundle Size Matters

markup
// Run in your local React project (npm install required)
// Vite example: install and configure a visualizer plugin
// npm install --save-dev rollup-plugin-visualizer

// vite.config.js
import { visualizer } from 'rollup-plugin-visualizer';
export default { plugins: [visualizer({ open: true })] };

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

एक Bundle Visualization पढ़ना

एक bundle visualizer typically एक interactive treemap produce करता है, जहां हर rectangle की size represent करती है कि वह specific module final bundle में कितनी space लेता है।

यह immediately obvious बना देता है कि कौन सी dependencies (या आपके अपने code के parts) bundle size में सबसे बड़े contributors हैं।

Note: उन dependencies के लिए surprisingly large rectangles देखें जिनसे आप छोटा होना expect करते थे — यह अक्सर एक sign है कि आप किसी library का actually ज़रूरत से ज़्यादा import कर रहे हैं।
Warning: एक dependency के बजाय 'आपके अपने code' का एक बड़ा chunk एक library swap के बजाय React.lazy code-splitting के लिए एक अच्छा candidate की ओर point कर सकता है।

Bundle Size कम करना

एक बड़ा contributor spot करने के बाद common fixes में शामिल हैं: एक बड़ी library से सिर्फ उस specific function को import करना जिसकी ज़रूरत है (पूरी library के बजाय), एक heavy dependency को एक lighter alternative से replace करना, और non-critical code को React.lazy के पीछे move करना ताकि यह initial bundle का हिस्सा बिल्कुल न हो।

Note: कई popular libraries specifically इसमें मदद के लिए documented tree-shakeable import styles offer करती हैं (जैसे पूरे lodash package के बजाय import debounce from 'lodash/debounce')।
Warning: एक lighter alternative library में swap करना एक real code change है जिसका अपना learning curve और API differences हैं — actual bundle savings को उस migration cost के against weigh करें।

उदाहरण: Reducing Bundle Size

markup
// Run in your local React project (npm install required)
// Importing the whole library (larger bundle impact):
// import _ from 'lodash';
// _.debounce(fn, 300);

// Importing just what's needed (smaller bundle impact):
import debounce from 'lodash/debounce';
debounce(() => {}, 300);

⚠️ This example uses an npm package with no CDN build available here — run this in your local React project.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. पूरी library import करना जब इससे सिर्फ एक छोटा function actually उपयोग होता है, bundle को unnecessarily bloat करते हुए।
  2. यह notice न करना कि एक single large dependency bundle को dominate करती है जब तक actually इसे visualize न किया जाए।
  3. यह भूल जाना कि कुछ libraries common use cases के लिए smaller, tree-shakeable alternatives या lighter-weight substitutes offer करती हैं।
चैप्टर सारांश
  • Bundle analysis tools visualize करते हैं कि आपके app के final JavaScript bundle में actually क्या है, और हर piece कितनी space लेता है।
  • एक common tool rollup-plugin-visualizer या webpack-bundle-analyzer है, एक interactive treemap generate करते हुए।
  • बड़ी, rarely-used dependencies lazy-loading या lighter alternative से replacement के लिए prime candidates हैं।
  • Tree-shaking (dead-code elimination) ES module imports और इसे properly support करने वाली libraries के साथ सबसे अच्छा काम करता है।
ब्राउज़र सपोर्ट

Build-tool feature (Vite/webpack के साथ काम करता है); कोई specific React version requirement नहीं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.