# v8-compile-cache-lib

> Fork of [`v8-compile-cache`](https://www.npmjs.com/package/v8-compile-cache) exposed as an API for programmatic usage in other libraries and tools.

***

[![Build status](https://img.shields.io/github/workflow/status/cspotcode/v8-compile-cache-lib/Continuous%20Integration)](https://github.com/cspotcode/v8-compile-cache-lib/actions?query=workflow%3A%22Continuous+Integration%22)

`v8-compile-cache-lib` attaches a `require` hook to use [V8's code cache](https://v8project.blogspot.com/2015/07/code-caching.html) to speed up instantiation time. The "code cache" is the work of parsing and compiling done by V8.

The ability to tap into V8 to produce/consume this cache was introduced in [Node v5.7.0](https://nodejs.org/en/blog/release/v5.7.0/).

## Usage

1. Add the dependency:

```sh
$ npm install --save v8-compile-cache-lib
```

2. Then, in your entry module add:

```js
require('v8-compile-cache-lib').install();
```

**`.install()` in Node <5.7.0 is a noop – but you need at least Node 4.0.0 to support the ES2015 syntax used by `v8-compile-cache-lib`.**

## Options

Set the environment variable `DISABLE_V8_COMPILE_CACHE=1` to disable the cache.

Cache directory is defined by environment variable `V8_COMPILE_CACHE_CACHE_DIR` or defaults to `<os.tmpdir()>/v8-compile-cache-<V8_VERSION>`.

## Internals

Cache files are suffixed `.BLOB` and `.MAP` corresponding to the entry module that required `v8-compile-cache-lib`. The cache is *entry module specific* because it is faster to load the entire code cache into memory at once, than it is to read it from disk on a file-by-file basis.

## Benchmarks

See <https://github.com/cspotcode/v8-compile-cache-lib/tree/master/bench>.

**Load Times:**

| Module           | Without Cache | With Cache |
| ---------------- | ------------: | ---------: |
| `babel-core`     |       `218ms` |    `185ms` |
| `yarn`           |       `153ms` |    `113ms` |
| `yarn` (bundled) |       `228ms` |    `105ms` |

*^ Includes the overhead of loading the cache itself.*

## Acknowledgements

* The original [`v8-compile-cache`](https://github.com/zertosh/v8-compile-cache) from which this library is forked.
* `FileSystemBlobStore` and `NativeCompileCache` are based on Atom's implementation of their v8 compile cache:
  * <https://github.com/atom/atom/blob/b0d7a8a/src/file-system-blob-store.js>
  * <https://github.com/atom/atom/blob/b0d7a8a/src/native-compile-cache.js>
* `mkdirpSync` is based on:
  * <https://github.com/substack/node-mkdirp/blob/f2003bb/index.js#L55-L98>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://civictech-ou.gitbook.io/nova-docs/use-cases/node_modules/v8-compile-cache-lib.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
