---
title: "PSA: Cache your prettier writes"
date: "2025-07-22"
description: "Since Prettier version 2.7, there's a --cache option for faster formatting"
author: "Brian Smith"
canonical: "https://bsmth.de/caching-prettier-writes/"
tags: ["JavaScript","MDN"]
---

# PSA: Cache your prettier writes

If you're using Prettier in your projects, you might benefit from caching that's been introduced since version 2.7 (June 2022).
Let's have a look at how it works and how much faster it runs.

## Caching using the Prettier CLI

The functionality was added by [@sosukesuzuki](https://github.com/sosukesuzuki) in [prettier#12800](https://github.com/prettier/prettier/pull/12800), and it adds a `--cache` option:

```bash
prettier --write --cache ./files
```

You can use `-w` as a shorthand for `--write`:

```bash
# shorthand
prettier -w --cache ./files
```

**Watch out:** `-c` is short for `--check`, not `--cache`.
Combining `-c` and `-w` doesn't make sense ("check" and "write"), so everything under `./files` will be formatted and a cache will be evicted / deleted:

```bash
# This means `--write --check`, so DON'T DO THIS:
prettier -w -c ./files
```

That's a good illustration why verbose CLI options are better, especially in collaborative projects where someone else may have to guess what the shorthand means.

## Prettier's cache location

When you run Prettier with the `--cache` option, a cache (a minified JSON file) is created at `./node_modules/.cache/prettier/.prettier-cache`, which contains details about files that have been formatted.
You can also set the location to somewhere else if you want:

```bash
prettier --write --cache --cache-location=.my-prettier-cache ./files
```

And you can evict the cache by removing the file or running Prettier without the `--cache` option:

```bash
rm ./node_modules/.cache/prettier/.prettier-cache
# or
prettier --write ./files
```

## Caching strategies

There's two options for the caching strategy; `content` (the file contents) or `metadata`, which I believe is the file's last modified timestamp and size.
Metadata may change without file contents being changed, so caching on file **contents** could be slower on first run due to reading data from disk, but avoids checking and formatting files with metadata-only changes.

This appears to be inspired the [ESLint caching implementation](https://eslint.org/docs/latest/use/command-line-interface#--cache-strategy).
Thanks to the open nature of the project's development, it's apparent that there's [minimal real-world differences](https://github.com/prettier/prettier/pull/12800#discussion_r889602368) in those two strategies based on cherry-picked example sources.

Don't sweat the difference if you're only interested in performance.
If you're running Prettier in CI with caching enabled, you want to cache based on `content` as you're likely formatting a fresh clone whose 'last modified' timestamps are irrelevant.

## Performance impact on 14,000 files

I wrote about this before; this is the outcome of enabling Prettier's cache in `mdn/content`.
Running `prettier --write` v `prettier --write --cache` on 14,000 files comes out like so:

```bash
# without a cache
prettier --write .  98.97s user 10.11s system 126% cpu 1:26.14 total
# with a cache
prettier --write --cache .  14.82s user 7.73s system 129% cpu 17.396 total
```

So in this case, we go from 90 seconds to 17 seconds, which definitely helps.
And yes, I do format the whole repo occasionally when testing config changes, applying updates, and other weird scenarios.

Most authors aren't formatting the whole repo locally very often, with pre-commit hooks running on **staged files** only.
There's still a lot of linting and formatting tools running during regular development due to search-and-replace changes and bulk edits, for instance.

## Send it to /dev/null

Also it looks like you can shave off a precious second by sending everything to `/dev/null`:

```bash
prettier --write --cache . > /dev/null  14.74s user 7.29s system 134% cpu 16.424 total
```

Or by disabling logging:

```bash
prettier --write --cache --log-level=silent .  14.65s user 7.57s system 135% cpu 16.428 total
```

## Job well done

Well done to the author and the maintainers on the improvements.
GitHub says there's [9.9 million projects using Prettier](https://github.com/prettier/prettier/network/dependents), and the number is likely way higher, so I think there's a serious chunk of developer years saved and hours of CI avoided through good old caching.
Congrats :clap:, and happy formatting!

## See also

- [`mdn/browser-compat-data`](https://github.com/mdn/browser-compat-data) on GitHub
