---
schema_version: "1"
id: 16ab17b7989abb2933161cf6e60b2e7d446170bbc6f8696325b8e31597a1dd95
title: Optimize JavaScript
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/salesforce-pages-developers-guide/guides/pages/best-practices/best-practices-performance/pages-best-practices-perf-javascript.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-pages-developers-guide/guide/pages-best-practices-perf-javascript.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: e3198ea1e757e19f397256336b5463cf8b7f95e728e15d992dbe6df8db2f4835
products:
  - Visualforce Developer Guide
  - Platform Developer Content
---

# 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.

    **note:** Only move JavaScript to the bottom of the page if you’re certain that it doesn’t have any adverse effects. For example, don’t move JavaScript code snippets that require `document.write` or event handlers from the `<head>` element.

-   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.

**See Also**  


-   [Using Static Resources](https://developer.salesforce.com/docs/platform/resources/pages-resources.md)

