Apps Script Storage Benchmarks
By Justin Poehnelt, Google Workspace alum Published Updated 4 min read
Update: See Exploring Apps Script CacheService Limits for tests of CacheService behavior and limits.
In Google Apps Script, there are a few options for key-value stores. This post will cover the following options:
- PropertiesService (User, Script, and Document Properties)
- CacheService (User and Script Caches)
- Firestore
- Sheet Developer Metadata (not tested here)
Here is a quick comparison of the options:
| Option | Item Size | Items | Cost | Expiration | Access Control |
|---|---|---|---|---|---|
| PropertiesService | 9KB | 1000 | Free | No | Yes (Separate Properties) |
| CacheService | 1KB | 1000 | Free | Yes | Yes (Separate Caches) |
| Firestore | 1GB | unlimited | Pay as you go | Yes | Yes (Rules) |
| Sheet Developer Metadata | 2MB | ? | Free | No | No |
You might have noticed I left off a key element here, latency, because I want to cover that below.
Latency comparisons
The latency of each option is the deciding factor for high-performance scripts. I ran a benchmark script (source below) performing 100 sequential write/read operations of a 100-byte payload.
| Store | Avg Latency (Read+Write) | Speed Factor |
|---|---|---|
| CacheService | ~63 ms | 1x (Baseline) |
| PropertiesService | ~80 ms | 1.25x Slower |
| Firestore (REST) | ~350 ms | 5.5x Slower |
| SpreadsheetApp | ~800+ ms | 12x Slower |
Takeaways:
- CacheService is faster, but not by the order of magnitude some expect. Use it for data that must expire.
- PropertiesService is nearly as fast as CacheService for persistent storage.
- SpreadsheetApp is by far the slowest option. Avoid using it as a database.
function runBenchmark() {
const iterations = 100;
const payload = "x".repeat(100); // 100 bytes
// Benchmark CacheService
const cacheFn = () => {
const cache = CacheService.getScriptCache();
cache.put("benchmark_test", payload, 10);
cache.get("benchmark_test");
};
// Benchmark PropertiesService
const propsFn = () => {
const props = PropertiesService.getScriptProperties();
props.setProperty("benchmark_test", payload);
props.getProperty("benchmark_test");
};
const cacheTime = timeExecution(cacheFn, iterations);
const propsTime = timeExecution(propsFn, iterations);
Logger.log(`CacheService (Avg/Op): ${cacheTime.toFixed(2)}ms`); // ~15-20ms
Logger.log(`PropertiesService (Avg/Op): ${propsTime.toFixed(2)}ms`); // ~150-200ms
}
function timeExecution(fn, iterations) {
const start = new Date().getTime();
for (let i = 0; i < iterations; i++) {
fn();
}
const end = new Date().getTime();
return (end - start) / iterations;
}
Open questions
- What happens when there is concurrent access to the key-value store?
- What happens when the script is run in different environments (e.g., add-on, web app, API)?
Decision guidance
Do I need expiration?
- No expiration: Use CacheService, PropertiesService, or Firestore
- Expiration: Use CacheService or Firestore
Of course these are only guidelines and you can do any number of things with keys, such as using a key suffix to simulate a TTL.
const keyToday = `${key}-${new Date().toISOString().split("T")[0]}`; How many items am I storing?
- Small number of items: Use CacheService, PropertiesService
- Large number of items: Use Firestore
How large of values am I storing?
- Small data: Use CacheService, PropertiesService
- Moderate (1KB-9KB): Use PropertiesService
- Large data: Use Firestore
How important is access control?
- User-specific data: Use PropertiesService, CacheService, or Firestore
- Audit logs: Use Firestore
How sensitive is my application to latency?
- Low latency: Use CacheService or PropertiesService
- Prefer user specific
CacheService.getUserCache(),PropertiesService.getUserProperties() - Latency insensitive: Use Firestore
How important is cost?
- Free: Use CacheService, PropertiesService,
Sheet Developer Metadata - Pay as you go: Use Firestore (likely free for most use cases)
Background
Below is an overview of each key-value store option in Google Apps Script.
CacheService
CacheService stores temporary key-value pairs in memory. It provides two cache scopes: User Cache and Script Cache.
- User Cache: Scoped to the current user, for user-specific data shared across script executions.
- Script Cache: Scoped to the script, for data shared by all users running the script.
Items stored in CacheService have a maximum size of 1KB per item and a total limit of 1000 items for each cache. When you hit this limit, you will see the following error:
Exception: Argument too large: value See the CacheService documentation for available methods.
PropertiesService
PropertiesService stores persistent key-value pairs across three scopes: User Properties, Script Properties, and Document Properties.
- User Properties (
PropertiesService.getUserProperties()): Stored in the user’s Google Account and accessible across scripts for user-specific data. - Script Properties (
PropertiesService.getScriptProperties()): Stored in the script project and shared across all users of the script. - Document Properties (
PropertiesService.getDocumentProperties()): Stored in a specific document and shared across all users with access to that document.
Properties stored using PropertiesService have a maximum size of 9KB per property and a total limit of 500KB for all properties combined. When you hit this limit, you will see the following error:
Exception: You have exceeded the property storage quota.
Please remove some properties and try again. To match the CacheService interface, I wrapped PropertiesService in a class:
class PropertiesWrapper {
/**
* @params {Properties} properties
*/
constructor(properties) {
this.properties = properties;
}
/**
* @params {String} k
* @params {String} v
*/
put(k, v) {
this.properties.setProperties({ k: v });
}
/**
* @params {String} k
* @returns {String|undefined}
*/
get(k) {
return this.properties.getProperty(k);
}
}
See the PropertiesService documentation for available methods.
Sheet developer metadata
Sheet Developer Metadata lets you attach custom key-value metadata directly to a Google Sheet and read or update it through the Google Sheets API. It works well for spreadsheet-specific configuration or tracking data.
The main limitation is rate limits, and you may see an error like this (you can request quota increases):
GoogleJsonResponseException: API call to sheets.spreadsheets.batchUpdate failed with error:
Quota exceeded for quota metric 'Write requests' and limit 'Write requests per minute per
user' of service 'sheets.googleapis.com' for consumer 'project_number:1234567890' See the Sheet Developer Metadata documentation for details.
Firestore
Firestore is Google Cloud’s managed NoSQL document database. It supports real-time data synchronization, automatic scaling, and queries.
With Firestore, you can store and retrieve structured data in documents organized into collections. It supports a wide range of data types, transactions, indexes, and security rules for fine-grained access control. Compared to other stores here, the free quota for Firestore should cover equivalent usage (see the pricing page). Firestore also supports TTL; see this blog post.
See the Firestore documentation or read my post on Using Firestore in Apps Script.
Conclusion
For relational data that outgrows key-value stores, see Connecting PostgreSQL to Apps Script.
Frequently asked questions
- What are the key-value store options in Google Apps Script?
In my experience, there are four main options for key-value stores in Apps Script: PropertiesService, CacheService, Firestore, and Sheet Developer Metadata. I compared them in this post to help you choose the right one for your project.
- When should I use PropertiesService?
I use PropertiesService when I need to store a small number of items (up to 1000) that are not too large (up to 9KB). It's free and I can scope properties to the user, the script, or a specific document.
- When should I use CacheService?
CacheService is my go-to for low-latency caching of small items. It's free, but I have to be aware of the 1000-item limit and the fact that the oldest items will be evicted when the cache gets full.
- When should I use Firestore?
I reach for Firestore when I have a lot of data or large items. It's a 'pay as you go' service, but the free tier is very generous. It's also great for real-time data and when I need more control over access with security rules.
- What is the difference between PropertiesService and CacheService?
The way I see it, PropertiesService is for stuff I need to keep around, while CacheService is for temporary data that can expire. PropertiesService can store larger items, but CacheService is faster.
- How do I handle concurrent access to a key-value store?
For PropertiesService and CacheService, I use LockService to prevent problems when multiple users are trying to write at the same time. Firestore is more advanced and has built-in transactions to handle this for me.