Cache Management in Puppeteer: Disabling, Clearing, and Performance Optimization
Optimize Puppeteer performance by effectively managing cache: disable, clear, and implement smart caching techniques for faster automation.

Want faster Puppeteer automation? Managing browser cache is key. This guide covers how to disable, clear, and optimize cache for better performance.
Key Takeaways:
- Disabling Cache: Use
setCacheEnabled(false)or browser launch flags like--disable-cacheto simulate fresh page loads. - Clearing Cache: Use
Network.clearBrowserCachevia Chrome DevTools Protocol (CDP) for clean test environments. - Smart Caching: Reduce data transfer by up to 92% with custom caching logic and in-memory storage.
- Performance Boost: Block unnecessary resources like images or ads to speed up tests and save bandwidth.
Efficient cache management can dramatically reduce data usage, improve test accuracy, and speed up automation workflows. Dive in to learn how!
Puppeteer Tutorial #4 | Launch Browser with Options
Turning Off Cache in Puppeteer
Disabling the cache in Puppeteer can be helpful for testing and automation tasks where fresh page loads are needed. Here's how you can do it and what to keep in mind.
Using the setCacheEnabled() Method
You can turn off caching in Puppeteer with the setCacheEnabled() method:
await page.setCacheEnabled(false);
Run this command before navigating to any page. By default, caching is on, so you need to disable it when your tests require a clean load of resources. For a more browser-wide solution, check out the next section.
Browser Launch Flags for Cache
To disable caching at the browser level, launch Chromium with specific flags:
const browser = await puppeteer.launch({
args: ['--disable-cache']
});
This method works well when you need to control caching for the entire browser session, complementing the setCacheEnabled() approach.
What Happens When You Disable Cache?
When the cache is off, every resource is downloaded fresh, which can slow things down and increase data usage. For example, tests on CNN's website showed an 88% jump in data transfer when caching was disabled [1]. To strike a balance between accuracy and performance, consider these tips:
- Use Chrome DevTools to check if the page content is cacheable.
- Add in-memory caching for specific resources if applicable.
- Only disable the cache when your test scenario demands it.
- Keep an eye on network reliability when the cache is off.
Disabling the cache is great for simulating first-time user behavior, but weigh the trade-offs based on your testing goals.
Removing Cache Data in Puppeteer
Automated tests often need a cleared cache to maintain consistent results.
Clearing Cache with setCacheEnabled()
You can clear cache data using Chrome DevTools Protocol (CDP) commands:
const client = await page.target().createCDPSession();
await client.send('Network.clearBrowserCache');
await page.setCacheEnabled(false);
This approach clears the browser cache and disables caching, ensuring a clean slate for your automation tasks.
You can also clear both cache and cookies together:
const client = await page.target().createCDPSession();
await client.send('Network.clearBrowserCache');
await client.send('Network.clearBrowserCookies');
Handling Specific Storage Types
Sometimes, you might need to clear specific stored data instead of the entire cache. Here's how you can manage cookies:
// Clear all cookies
const cookies = await page.cookies();
await page.deleteCookie(...cookies);
// To delete a specific cookie, use:
// await page.deleteCookie({ name: 'cookie_name', url: 'https://example.com' });
// Set cookies to expire
const cookies = await page.cookies();
for (let cookie of cookies) {
cookie.expires = -1;
}
await page.setCookies(...cookies);
This allows precise control over cookie management during your tests.
Managing Cache in Multiple Tabs
When working with multiple tabs, it's a good idea to isolate cache data by using separate browser contexts. Here's how:
const browser = await puppeteer.launch();
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
const client = await page.target().createCDPSession();
await client.send('Network.clearBrowserCache');
// Close the context after tasks are done
await context.close();
Using separate contexts prevents cache interference between tabs, making it ideal for running parallel tests.
sbb-itb-23997f1
Cache Settings for Better Speed
Managing cache effectively in Puppeteer can cut data transfer by up to 92% [1], making automation much faster.
Smart Cache Usage
To balance speed and up-to-date data, you can intercept requests and responses to implement smarter caching. Here's an example:
const cache = new Map();
async function handleRequest(request) {
const url = request.url();
if (cache.has(url)) {
const cachedResponse = cache.get(url);
if (isFresh(cachedResponse)) {
return request.respond(cachedResponse);
}
}
// Continue the request if it's not cached
request.continue();
}
async function handleResponse(response) {
const headers = response.headers();
if (headers['cache-control'] && headers['cache-control'].includes('max-age')) {
const responseData = {
status: response.status(),
headers: headers,
body: await response.buffer()
};
cache.set(response.url(), responseData);
}
}
This setup minimizes unnecessary network requests while keeping essential data updated by validating the cache-control header.
Building Custom Cache Rules
Tailor caching to your needs by creating specific rules. For instance:
const customCacheRules = {
shouldCache: (response) => {
const headers = response.headers();
return headers['cache-control'] &&
headers['cache-control'].includes('max-age') &&
Number(headers['cache-control'].match(/max-age=(\d+)/)[1]) > 0;
},
getExpirationTime: (headers) => {
const maxAge = headers['cache-control'].match(/max-age=(\d+)/)[1];
return Date.now() + (parseInt(maxAge) * 1000);
}
};
These rules help determine which responses to cache and how long to keep them.
Checking Cache Performance
Once your caching rules are in place, evaluate their impact using performance metrics:
const metrics = {
totalRequests: 0,
cachedResponses: 0,
dataSaved: 0
};
async function trackCacheMetrics(request, response) {
metrics.totalRequests++;
if (response.fromCache()) {
metrics.cachedResponses++;
metrics.dataSaved += parseInt(response.headers()['content-length'] || 0);
}
}
Track key metrics like total requests, cached responses, and data saved. Here's a comparison based on testing [1]:
| Metric Type | Without Cache | With Cache | Improvement |
|---|---|---|---|
| Data Transfer | 177 MB | 13.4 MB | 92% reduction |
These results highlight how well-designed caching can drastically improve Puppeteer's performance.
Common Issues and Solutions
Fixing Cache Problems
When using Puppeteer, enabling request interception disables the browser's native caching. This can lead to higher data transfer and slower page load times [1]. To address this, you can implement custom caching with the following approach:
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Initialize cache storage
const responseCache = new Map();
await page.setRequestInterception(true);
page.on('request', async request => {
const url = request.url();
if (responseCache.has(url)) {
await request.respond(responseCache.get(url));
return;
}
request.continue();
});
page.on('response', async response => {
const url = response.url();
const headers = response.headers();
if (headers['cache-control'] && headers['cache-control'].includes('max-age')) {
responseCache.set(url, {
status: response.status(),
headers: headers,
body: await response.buffer()
});
}
});
To avoid potential memory leaks, make sure to clean up resources effectively:
async function cleanupResources(page) {
await page.removeAllListeners();
const client = await page.target().createCDPSession();
await client.send('Network.clearBrowserCache');
await client.detach();
await page.close();
}
By combining these techniques, you can reduce overhead and improve Puppeteer's performance.
Cache Management Tips
Here are some practical tips for managing cache more effectively, based on testing and analysis:
| Issue | Solution | Impact |
|---|---|---|
| High Data Transfer | Use in-memory caching | Reduces traffic by up to 92% [1] |
| Resource Leaks | Apply cleanup procedures | Helps prevent memory exhaustion [3] |
| Slow Page Loads | Block unnecessary resources | Improves rendering speed significantly [2] |
For better performance, you can block certain resources like images or stylesheets to speed up page loading:
const browserOptions = {
userDataDir: './cache-directory',
args: [
'--disable-background-timer-throttling',
'--disable-extensions'
]
};
await page.setRequestInterception(true);
page.on('request', request => {
if (request.resourceType() === 'image' || request.resourceType() === 'stylesheet') {
request.abort();
} else {
request.continue();
}
});
Using these strategies can streamline your Puppeteer workflows while keeping resource usage under control.
Conclusion
Efficient cache management in Puppeteer can dramatically improve performance while reducing resource usage. This guide has covered how to disable, clear, and adjust cache settings to achieve better results. Below is a concise summary of the main strategies and their effects.
Summary Points
Testing has shown how effective proper cache management can be [1], emphasizing the importance of handling it carefully.
Here’s a quick look at some key strategies and their outcomes:
| Strategy | Implementation | Performance Impact |
|---|---|---|
| In-Memory Caching | Cache responses with max-age > 0 | 92% reduction in data transfer [1] |
| Resource Blocking | Disable ads and tracking scripts | Noticeable page load improvement [2] |
| Smart Screenshot Timing | Use waitForSelector() | Faster rendering completion [2] |
| Cross-Session Caching | Configure userDataDir | Retains CSS/JS/image assets [2] |
Key Implementation Tips
- Asset Optimization: Compress assets and optimize images to minimize HTTP payloads [2].
- Precise Timing: Take screenshots exactly when content is ready, avoiding unnecessary delays [2].
- Memory Efficiency: Use Buffer operations instead of file system writes to speed up processing [2].
"When optimizing Puppeteer, remember that there are only so many ways to speed up the startup/shutdown performance of Puppeteer itself. Most likely, the biggest speed gains will come from getting your target pages to render faster." - Jon Yongfook, Founder, Bannerbear [2]
Related posts
- Installing and Configuring Puppeteer: Solving Common Dependency and Chromium Issues
- Mastering Page Navigation with Puppeteer: Effective Use of goto and Navigation Options
- Browser Automation with Puppeteer and JavaScript: Practical Implementation in Node.js
- Cookie Management in Puppeteer: Session Preservation, Auth Emulation, and Limitations



