Optimize JavaScript
To ensure
efficient delivery to the client,
optimize
the JavaScript in Visualforce
pages
Optimized JavaScript also improves caching and
reduces
load times.
To improve the JavaScript in a Visualforce page:
- Externalize JavaScript files. This process increases the number of initial HTTP requests, but it also reduces the size of individual pages and takes advantage of browser caching.
- Build custom versions of JavaScript libraries with only the functions that you need. This process significantly reduces the size of a JavaScript file. Many open-source JavaScript libraries, such as jQuery, provide this option.
- Reduce HTTP requests by combining all JavaScript files into a single file. Remove duplicate functions that can result in more than one HTTP request.
- Remove comments and whitespace. Compress the resulting file for faster downloads.
- Use static resources to serve JavaScript files. JavaScript served this way benefits from the caching and the content distribution network (CDN) built into Salesforce.
- Put scripts at the bottom of the page. If the scripts load directly before the closing </body> tag, the page can download other components first and render the page progressively.
- Instead of using the <apex:includeScript> tag, consider using a standard HTML <script> tag directly before your closing </apex:page> tag. The <apex:includeScript> tag places JavaScript right before the closing </head> element, which causes the browser to attempt to load the JavaScript before rendering any other content on the page.