Introduction
Byte for byte, JavaScript is the most expensive resource on the web.
Unlike an image file that the browser merely decodes and paints, a 1MB JavaScript bundle must be downloaded over the network, decompressed, parsed, compiled, and executed on the CPU.
On budget mobile devices with modest processors, heavy JavaScript scripts freeze the main thread for several seconds, rendering the page unresponsive to taps.
Here is how modern frontend engineers optimize JavaScript execution budgets.

Dynamic Imports and Route-Based Code Splitting
Never ship a single monolithic bundle that includes every script needed across your entire application.
A visitor browsing your homepage should not download the heavy charting library used exclusively on your admin analytics dashboard.
Use dynamic ES module imports (import('./heavyModule.js')) so that complex scripts (date pickers, 3D canvases, PDF generators) are loaded only when the user requests that feature.
Route-based splitting cuts initial bundle payloads by 50% to 80%.
Tree Shaking and Auditing Unused Dependencies
Many developers import an entire 70KB utility library like lodash just to use a single debounce() helper function.
Ensure your bundler (Vite, Rollup, or webpack) is configured with ES modules (import/export) to enable tree shaking, which strips unused code during compilation.
Inspect your bundles with tools like bundle-analyzer or source-map-explorer to spot accidental bloat.
Replace legacy bulky libraries with lightweight modern alternatives (e.g. date-fns instead of moment.js, or native browser Intl APIs).
Deferring Non-Critical Scripts with type='module'
Ensure non-critical scripts carry the defer attribute or use native <script type="module">.
Deferred scripts download in parallel with HTML parsing without blocking the browser, executing in exact document order right after DOM parsing is complete.
This ensures your visual content paints immediately before JavaScript execution begins.
Total Blocking Time (TBT) drops to near zero.
Frequently Asked Questions
What is a reasonable JavaScript budget for a business website?: Aim for less than 150KB of gzipped JavaScript for initial page load on mobile viewports.
Does minification reduce execution time?: Minification reduces download bandwidth and parsing time, but architectural code splitting is required to reduce CPU execution time.
How do I profile JavaScript bottlenecks in Chrome?: Open Chrome DevTools > Performance panel, record a page reload, and inspect the 'Main' thread flame chart for yellow Long Task blocks.