惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

Datadog | The Monitor blog

How to audit and clean up monitors effectively How we made a SQL query optimization agent 59% more accurate using autoresearch and LLM Observability Reduce CVE noise with OpenVEX assessments in Datadog Diagnose slow PostgreSQL queries faster with explain plan correlation Explore Datadog metrics with Natural Language Queries Attribute AI costs across providers with Datadog Cloud Cost Management Simplify micro-frontend observability with Datadog RUM Toto 2.0: Time series forecasting enters the scaling era Diagnose and resolve database performance issues faster with Database Investigator Datadog for Government achieves FedRAMP® High certification Analyze cloud costs with flexible spreadsheets in Datadog Sheets Inside Datadog’s AI Research Lab: Meet two PhD candidates behind Toto Connect triage and investigation in a single workflow with Datadog Cloud SIEM This Month in Datadog - April 2026 Monitor and optimize Supabase query performance with Datadog Database Monitoring Add dynamically updating context to logs with Reference Tables and Observability Pipelines Introducing ARFBench: A time series question-answering benchmark based on real incidents Test network paths with TCP, UDP, and ICMP in Datadog The product signal latency gap slowing your growth How to investigate cloud credential compromise with Bits AI Security Analyst Evaluate, optimize, and secure your Google Cloud AI stack with Datadog Turn developer feedback into operational insight with Datadog Forms and Sheets Identify and fix code issues faster with Datadog’s Azure DevOps Source Code integration Steganography at scale: Embedding share URLs in Datadog widget screenshots Bringing observability data hosting to the UK on AWS Centralize observability management with Datadog Governance Console Every team should be A/B testing Manage service tracing across hosts with Single Step Instrumentation rules Route OTel data from AI apps to ClickHouse and Datadog using Observability Pipelines Spotting CI/CD misconfigurations before the bots do: Securing GitHub Actions with Datadog IaC Security Detect runtime threats in Python Lambda functions with Datadog AAP Offline evaluation for AI agents: Best practices Introducing our open source AI-native SAST Instrument and monitor Boomi integration flows with OpenTelemetry and Datadog Not all index scans are equal: How we cut query latency by over 99% Platform engineering metrics: What to measure and what to ignore Integrate Recorded Future threat intelligence with Datadog Cloud SIEM CI/CD security: threat modeling using a MITRE-style threat matrix CI/CD security: How to secure your GitHub ecosystem Ingress NGINX is EOL: A practical guide for migrating to Kubernetes Gateway API How we built a real-world evaluation platform for autonomous SRE agents at scale Operating agentic AI with Amazon Bedrock AgentCore and Datadog LLM Observability: Lessons from NTT DATA Introducing the Datadog Code Security MCP Capture and analyze custom heatmaps in Session Replay Understand session replays faster with AI summaries and smart chapters Monitor ClickHouse query performance with Datadog Database Monitoring How we designed empathetic alert sounds for on-call engineers Search and act across Datadog to resolve issues faster with Bits Assistant Measure the business impact of every product change with Datadog Experiments Analyzing round trip query latency Introducing Bits AI Dev Agent for Code Security Datadog achieves ISO 42001 certification for responsible AI Monitor Nutanix clusters, hosts, and VMs with Datadog Monitor Juniper Mist in Datadog A new Host Map for modern infrastructure When upserts don't update but still write: Debugging Postgres performance at scale Annotate traces to improve LLM quality with Datadog LLM Observability What's new in Cloud SIEM: AI-powered investigations, enhanced threat intelligence, and scalable security operations Explore Kubernetes with native OpenTelemetry data Monitor Oracle Fusion Cloud Applications with Datadog Announcing the Datadog Terraform provider v4.0.0 Scaling Kubernetes workloads on custom metrics How to design cloud environments for AI-powered threat analysis Monitor Aruba Central in Datadog How we centralize and remediate risks with Datadog Case Management Accelerate incident response with Datadog and ServiceNow Monitor your application and network load balancer logs Understanding Karpenter architecture for Kubernetes autoscaling Tools for collecting metrics and logs from Karpenter Monitor Karpenter with Datadog What your product data is actually saying Key metrics for monitoring Karpenter Securing Datadog's platform in the AI age: The role of observability data Closing the verification loop: Observability-driven harnesses for building with agents Closing the verification loop, Part 2: Fully autonomous optimization When an AI agent came knocking: Catching malicious contributions in Datadog’s open source repos Four ways engineering teams use the Datadog MCP Server to power AI agents Approaching your observability migration with the right mindset Meet the new Bits AI SRE: Deeper reasoning, twice as fast Designing MCP tools for agents: Lessons from building Datadog's MCP server Key learnings from the 2026 State of DevSecOps study Use plain English to query your multi-cloud infrastructure in Resource Catalog Simplifying troubleshooting across the user journey with Datadog Synthetic Monitoring Protect your OCI resources with Datadog Cloud Security This Month in Datadog - February 2026 Fine-tune Toto for turbocharged forecasts Amazon EC2 security: How misconfigured and public AMIs expand your cloud attack surface Enable end-to-end visibility into your Java apps with a single command Measure and improve mobile app startup performance with Datadog RUM Evaluating our AI Guard application to improve quality and control cost Identify untested code across every level of your codebase Make use of guardrail metrics and stop babysitting your releases Monitor Versa Networks SD-WAN performance in Datadog How we reduced the size of our Agent Go binaries by up to 77% Improve performance and reliability with APM Recommendations Remediate transitive vulnerabilities faster with Datadog Software Composition Analysis Generate audit-ready vulnerability and compliance reports with Datadog Sheets Monitor Fortinet FortiManager performance in Datadog Improve test coverage across codebases with Datadog Code Coverage Move fast, don’t break things: Consistent testing standards at scale
Configuring JavaScript caches for better performance
2026-03-27 · via Datadog | The Monitor blog
Addie Beach

Addie Beach

Chumeng Xu

Chumeng Xu

Rachel White

Rachel White

Caching is critical to modern apps, helping you serve data more quickly and improve your app’s overall performance. Effective caches can result in better Core Web Vitals (CWV), improved search visibility, and a smoother user experience.

Deciding which types of caches to use in your app can be challenging, especially for apps that rely heavily on JavaScript code. These apps often involve resource-intensive calculations, repeated API calls, or complex visualizations and animations, all of which come with performance costs. JavaScript caches can help you offset the performance impact of these complicated components and improve your app’s processing speed. Yet, with so many different types of data being processed, you may have to use a combination of caching methods to achieve the best results.

In this post, we’ll discuss common types of JavaScript caches, including which ones are best suited to dynamic data or static data or work well for both data types. We’ll also touch on how you can measure the success of your caching strategy.

JavaScript cache types

Caching data effectively means balancing multiple factors, including performance, freshness, and security. For example, serving the latest data means users get more accurate information. However, the frequent downloads necessary for doing so can cause significant latency, and your app may require more compute resources and higher network throughput to handle the volume of requests.

Navigating these tradeoffs means deciding how long cached data can be considered valid (its staleness tolerance) and how you’ll refresh this data once it becomes too stale. A cache that uses a set time to live (TTL) to automatically invalidate stale data is often easier to configure but may lead to a lag in data updates. Meanwhile, cache invalidation strategies ensure that your cache updates as soon as fresh data is available but can be more complicated to implement and may result in performance impacts through frequent origin calls.

You’ll also want to consider the security needs of your data. Many JavaScript caches store data on the user’s device for faster retrieval. Because client-side storage is accessible to JavaScript running on the page, it is vulnerable to XSS attacks, browser extensions, and access by other users on shared devices. To avoid exposure, you’ll want to limit the amount of sensitive data you store in these caches, such as private keys, authentication tokens, or payment information.

JavaScript apps often contain both static and dynamic resources, which may benefit from being cached separately. Let’s explore a few different methods of caching, which data types they’re best suited for, and how they handle key tradeoffs.

Caching methods summary

Cache typeBenefitsDrawbacksData types
In-memoryFast and easy to implementHardware limitations, short persistence, prone to stalenessNotifications, scroll positions
sessionStorageMore storage capacity and control than cookiesShort persistence, blocks the main threadPage history, menu options
localStoragePersistent storage for simple, small dataCapacity limitations, blocks the main threadUser preferences, accessibility settings
IndexedDBOffline, persistent storage for large, structured data setsComplicated API, limited browser supportChat messages, time-series metrics
HTTP cachingStraightforward options for data access and invalidationPotential data leakage or staleness, depending on header configurationImages, index.html files
CDNsFast access to shared resourcesPrivacy concerns with misconfigured communication policiesImages, index.html files
Cache APIRelatively large, persistent storage with flexible caching optionsPotential performance costsCSS code, product information
OPFSVery large, persistent storageLimited browser supportGame assets, machine learning data

Dynamic data caching

JavaScript code often involves handling data that changes frequently, i.e., dynamic data. This can include the results of API requests, database queries, complex computations, and user input. Knowing which method of caching to use for dynamic data means considering its size, structure, and staleness tolerance.

In-memory caches

In-memory caches store data temporarily within a device’s RAM. These caches provide fast performance since data is available directly within the user’s device and isn’t mediated by the network.

In-memory caches usually only persist while your JavaScript code is running, meaning that any data stored in them disappears once the user refreshes or navigates away from the page. They also lack built-in cache invalidation, which can lead to stale data. Overreliance on them can cause slowdowns and out-of-memory (OOM) errors within your app.

Given these tradeoffs, in-memory caches are best suited for data that needs to be served quickly and stored only briefly. For JavaScript data, this can include notifications, scroll positions, and precalculated historical statistics. In-memory caching can also be particularly useful for single-page apps (SPAs), as it provides a good system for storing routing information for fast navigation.

To reduce memory consumption by unnecessary values or metadata, some developers use WeakMaps to implement these caches. Here’s a simple in-memory cache created with a WeakMap that provides functionality for checking, retrieving, setting, or removing cached values:

const cache = new WeakMap();

function has(key) {

if (key !== null && typeof key === "object") {

return cache.has(key);

}

return false;

}

function remove(key) {

if (key !== null && typeof key === "object") {

return cache.delete(key);

}

return false;

}

function getOrSet(key, compute) {

if (key === null || typeof key !== "object") {

throw new TypeError("Cache key must be an object");

}

if (cache.has(key)) {

return cache.get(key);

}

const value = compute();

cache.set(key, value);

return value;

}

The Web Storage API

The Web Storage API offers longer persistence and more explicit read/write control over your data than in-memory caches. This API provides two storage mechanisms:

  • sessionStorage persists data while the current browser session is active
  • localStorage persists data even after the browser is closed

Neither are technically methods of caching, but they are often used for the similar purpose of storing small amounts of data. For sessionStorage, this may include navigational or UI context, such as currently expanded menus, previous pages, or the state of a multi-step form. localStorage’s longer persistence means it’s often more suited for user preferences, such as light or dark mode toggles, language selections, or accessibility settings. For relatively simple data types such as these, both options provide fast performance.

That being said, both sessionStorage and localStorage are synchronous, meaning that they block the main thread until their operations are complete. This can quickly lead to high latency when storing or retrieving large data sets or other long tasks. As a result, using these storage methods can cause rendering delays for important elements, such as critical user interactions. Therefore, many recommend using the Web Storage API mechanisms with caution—particularly localStorage, which can quickly accumulate data across sessions. As many browsers set a hard limit of how much you can store via this API, usually around 5-10 MiB, this data accumulation can fill up user storage, causing write operations to fail with a QuotaExceededError.

IndexedDB

To cache large amounts of structured data, you may want to use IndexedDB. IndexedDB is an API that enables you to create and manage client-side transactional databases. It is ideal for caching bigger data sets, such as chat messages, saved articles, and time-series metrics for data visualizations.

Because it provides offline, browser-based storage that can support a variety of data types, IndexedDB can be especially useful for progressive web apps (PWAs), which must be able to support offline use. However, the IndexedDB API is often considered challenging to use, as it has an older, callback-based structure; few native querying methods; and a confusing commit mechanism for transactions.

Additionally, IndexedDB transactions often take a while to process, which can lead to slow performance. This can be offset by the fact that IndexedDB works asynchronously, meaning that other browser processing operations aren’t blocked while large data processing activities are being carried out. Note that attempting to process too many large requests at the same time can quickly impact memory and disk I/O performance. Therefore, you’ll want to limit these requests to ensure that performance isn’t impacted.

Static data caching

In contrast to dynamic data, static assets don’t need to be updated as frequently. These resources often make up the skeleton of your application, such as index.html files, CSS code, and web fonts, as well as any JavaScript code and libraries necessary for basic user interactions. They may also include larger files that are unlikely to change often, including images and videos.

While freshness may not be as much of a concern for static data, you still need to consider data access restrictions and potential security risks. For example, caching static data in a proxy server can significantly reduce bandwidth use by limiting the number of calls made to the origin server. However, this data can be compromised during proxy communications if not encrypted properly.

HTTP headers are fields included in HTTP requests and responses that provide specifications about how data should be handled. You can configure these headers to store responses to user requests within a cache. Some header fields important to creating and managing these caches include:

  • Cache-Control: the Cache-Control header enables you to specify how resources should be stored and validated. There are a few directives you can use to customize this header:
    • max-age: the max-age value corresponds to your staleness tolerance. This is a more modern alternative to headers like Expires, which require you to set a specific date and time for data expiration. Instead, max-age is relative to when the request was received. For static resources, max-age should be set for a long timeframe—Chrome recommends a year.
    • public, private, and no-store: setting Cache-Control to public stores data within a proxy cache that is shared by multiple users. Meanwhile, the private directive stores data in a single-user cache located within the web browser. The latter can be a better choice for storing user-personalized content, as proxy caches pose a greater risk of accidental data leakage. Finally, you can also use the no-store directive to prevent highly sensitive information, such as personally identifiable information (PII) or user authentication information, from being cached at all.
  • ETag and Last-Modified: these headers enable you to track data changes for more granular cache invalidation. Last-Modified headers use timestamps to log updates. These timestamps have limited precision and may not detect rapid successive updates. By contrast, ETag headers use opaque identifiers, usually a hash. If using content-based hashing, your ETags will only change when the content updates (instead of whenever the file is saved), which can provide more reliable versioning than timestamps.

Content delivery networks (CDNs)

CDNs enable you to store static assets in shared proxy servers that are geographically close to users. By minimizing the distance that data needs to travel, CDNs help users more quickly access server-side resources. Additionally, many CDN caches contain assets that are shared across multiple users, reducing the number of requests made to app storage.

Many CDN services come with default cache settings already configured. Often, you can use HTTP headers to set these values yourself. For example, Cloudflare will automatically override its own out-of-the-box TTL settings if it detects that a custom Cache-Control header has been defined. Since your CDN caches route much of your data through third-party services, you should ensure that the traffic between clients, the CDN, and your origin is encrypted using Transport Layer Security (TLS).

Advanced data caching

A few caching methods work equally well for both static and dynamic data. These caching methods provide advanced functionality for controlling how your data is stored and when it’s accessed. As a result, they can be especially useful for building PWAs, which rely heavily on caching to dynamically serve content and provide robust offline access.

The Cache API and service workers

The Cache API is designed to store network requests and responses within the user’s browser. Usually, tasks involving the Cache API are managed by JavaScript service workers. These service workers use event listeners to intercept HTTP requests and then determine whether the requested data already exists in Cache API storage. If it does, the service workers fetch the data and deliver it to the user.

This API provides access to a number of flexible caching methods that are useful for fine-tuning dynamic data storage. For critical, real-time data, you can configure a network-first strategy that uses the cache only as necessary—e.g., in case of network issues—to improve your app’s reliability without sacrificing accuracy. Note that this strategy can come with performance tradeoffs, as it involves updating the cache with every request.

For data that rarely changes, you can use a stale-while-revalidate strategy. Stale-while-revalidate can lower your app’s latency by defaulting to the cache for incoming requests, then updating cached content in the background.

Due to its relatively long persistence and large storage capacity, the Cache API is also often used to store static assets. For this data, you’ll usually want to configure your app to rely primarily on the cache and only send requests over the network when the asset is not found. This is referred to as a cache-first strategy.

No matter which strategy you use, the Cache API can result in some degree of performance costs. Each network request means waiting for the service worker to process it and decide where to retrieve the data from. You can mitigate this by using the InstallEvent.addRoutes method in the Service Worker API to bypass the service workers and immediately fetch static resources you know the location of.

The Origin-Private File System (OPFS)

If you need a performant caching option for large files, you may want to use the OPFS. The OPFS is a persistent virtual file system hidden from the user. It offers better performance than storing files within the user-visible file system because it isn’t subject to the same permission prompts and security checks. The browser manages the OPFS directly, making it easily accessible by web apps.

The OPFS provides greater control over your files than databases like IndexedDB, enabling you to stream files and work with them on the byte level. This makes it best for caching particularly large or otherwise complex data. When it comes to dynamic data, this might include autosaved copies of user-generated content and chunked, in-progress file downloads. For static data, you might use the OPFS to cache images, videos, and game assets. This level of storage capability and flexibility also makes the OPFS good for caching machine learning data, including model weights, vector indexes, training logs, and tokenizers.

The OPFS is asynchronous when running on the main thread, which enables it to provide relatively low-latency processing. For even better performance, you can use JavaScript Web Workers to execute complex tasks on a background thread. Web Workers have access to OPFS’s synchronous API, making them well-suited for read/write-intensive file operations that would otherwise cause blocking. Meanwhile, other processing can continue uninterrupted. As with IndexedDB, you should monitor large requests closely to ensure that they don’t consume excess network resources. For example, you may want to create a monitor that evaluates the latency of requests involving the OPFS cache.

Use Datadog RUM to improve your cache setup

You don’t need to cache every resource in your app. Doing so can quickly run up against storage limits with in-memory caches or the Web Storage API, or add unnecessary complexity, particularly with IndexedDB or the OPFS. While some apps have either a clear need for fresh data or a high staleness tolerance, many fall somewhere in between. This makes it difficult to weigh the potential performance impacts of frequent network requests against the reliability impacts caused by serving outdated information via a cache.

Datadog can help you identify which resources are leading to high latency in your app and, therefore, which ones might benefit the most from caching. Let’s say that you’ve recently completed a UI redesign. Soon after, you receive an alert that the Largest Contentful Paint (LCP) score for your product purchase flow has increased, indicating slower interactions. With the Optimization overview in Datadog Real User Monitoring (RUM), you can quickly identify which pages in your app are experiencing the worst performance to pinpoint the source of the problem. These pages are sorted on view count by default, helping you determine which ones have the greatest user impact. To help you troubleshoot, the Optimization overview lists the CWV, First Contentful Paint (FCP), and overall loading time for each page.

The Optimization overview in Datadog RUM, with a list of the most popular pages that have performance issues displayed.

You see the cart page has a particularly bad LCP score, so you select this score to view a detailed summary of factors that contributed to its rating. This includes a list of large resources and their processing duration, along with the percentage of sampled sessions that included cached resources. This summary also highlights which elements contributed the most to the overall score so you can identify those that are impacted by large resource downloads. For LCP, this is the largest rendered element. In this case, you see that the largest elements are images for items within the user’s cart.

The LCP optimization view for a /cart page, with details about the largest rendered element displayed.

Finally, you can view an event waterfall that breaks down the timeline for all event executions on this page, with long tasks highlighted.

An event waterfall for the /cart page that highlights long tasks.

Here, you notice that one of the longest events involves an API call responsible for fetching these images. With this information, you can identify resources that could be particularly useful to cache. Because these images are unlikely to change often, you decide to store them in the Cache API using a cache-first strategy for faster rendering.

As you update your caching strategy, you should continue monitoring your app performance to ensure that the new caches are an improvement. The Optimization feature comes with a timeseries graph that helps you track trends in your frontend performance metrics over time. You can also set up monitors that will alert you if your app starts showing signs of poor performance, which can help you respond quickly if you’ve implemented a new cache incorrectly. For example, the Datadog RUM SDK will automatically collect duration information for Cache API fetch requests. By creating a monitor using the resource.duration metric, you can easily catch request latency that exceeds a predefined threshold:

The configuration page for a monitor that focuses on request latency.

In this example, you may want to separate cache request calls from network request calls in order to more closely evaluate the success of your new caching strategy. To do so, you can easily collect data on cache request latencies by instrumenting your code with the addDurationVital method:

datadogRum.addDurationVital('cache_api_lookup', {

startTime,

duration: performance.now() - startTime,

context: {

url: typeof request === 'string' ? request : request.url,

cache_hit: response?.fromCache === true,

}

});

You can then create a distribution metric from your vital.duration data and generate a cache-specific request latency monitor.

Cache for better performance

Waiting for critical web app data to download can quickly degrade your user experience. While it can be difficult to find the right combination of caches, optimizing these combinations can lead to meaningful improvements to your JavaScript app’s speed, reliability, and user experience.

To learn more about using Datadog to improve your caching strategy, you can view our RUM documentation. Or, if you’re new to Datadog, you can sign up for a 14-day free trial.