Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading caniuse-lite takes ~0.5sec, could the cache be persisted on disk? #1492

Closed
Knagis opened this issue Mar 9, 2023 · 10 comments
Closed

Comments

@Knagis
Copy link
Contributor

Knagis commented Mar 9, 2023

On my machine the require("caniuse-lite") can take around 450ms. This is relatively noticeable slowdown every time some compilation is started. This might be more than usual because i have ts-node etc. but overall my setup is rather straight forward webpack+postcss. Building the prefixes list takes another ~50ms.

I am planning to patch autoprefixer to persist the current in-memory cache to disk to avoid this. Would you be willing to consider this as a PR as well? (just so i know if i can cut corners or if i should make things properly, with configuration options etc.)

@ai
Copy link
Member

ai commented Mar 9, 2023

in-memory cache to disk to avoid this

require() should already have this cache. require() the module next time took less time (if there is no hacks around require()).

How do you plan to add better cache?

@Knagis
Copy link
Contributor Author

Knagis commented Mar 9, 2023

i want the cache to be persisted between compilation runs. so restarting compilation process would be slightly faster. it can also thus be shared between different processes (like production webpack runs and storybook builds etc.)

@Knagis
Copy link
Contributor Author

Knagis commented Mar 9, 2023

const started = Date.now();
let { agents } = require('caniuse-lite')
let dataPrefixes = require('../data/prefixes')
console.log("caniuse require took", (Date.now() - started), "ms");

Running node node_modules\autoprefixer\lib\autoprefixer.js (node 16.19.0):

caniuse require took 423 ms
caniuse require took 327 ms
caniuse require took 324 ms
caniuse require took 320 ms
caniuse require took 331 ms
caniuse require took 334 ms
caniuse require took 338 ms

@ai
Copy link
Member

ai commented Mar 9, 2023

Yes, I understand the problem (honestly, it mostly came from ts-node because it parses all require files, and Can I Use DB has a big JSONs).

What solution do you suggest?

@Knagis
Copy link
Contributor Author

Knagis commented Mar 9, 2023

the times above are plain node, without ts-node.

The let autoprefixerData = { browsers: agents, prefixes: dataPrefixes } object serialized to JSON takes around 285KB in my case. So I was thinking of writing this to disk (probably, just based on caniuse-lite/package.json version) and reading when available.

@Knagis
Copy link
Contributor Author

Knagis commented Mar 9, 2023

First small thing - changing

let { agents } = require('caniuse-lite')

to

let { agents } = require('caniuse-lite/dist/unpacker/agents')

removes the big cost to load whole caniuse-lite for loading just browser stats - this same import is used by browserslist to calculate usage.

So that leaves only the feature require.

@ai
Copy link
Member

ai commented Mar 9, 2023

So I was thinking of writing this to disk (probably, just based on caniuse-lite/package.json version) and reading when available

You need to think about cache invalidation on caniuse-lite update. And about working in the environment without fs (for instance, in the browser).

It could be much more complex, than you think.

let { agents } = require('caniuse-lite/dist/unpacker/agents')

Yes, we can change it. Send PR.

@Knagis
Copy link
Contributor Author

Knagis commented Mar 9, 2023

And changing

let unpack = require('caniuse-lite').feature

to

let unpack = require('caniuse-lite/dist/unpacker/feature');

results in total time of 100ms.

So without any caching, these two imports reduce startup time from ~300ms to ~100ms.

@Knagis
Copy link
Contributor Author

Knagis commented Mar 9, 2023

PR incoming.

ai added a commit that referenced this issue Mar 9, 2023
)

* Improves startup time by requiring specific caniuse files (#1492)

* Update lib/browsers.js

Co-authored-by: Andrey Sitnik <andrey@sitnik.ru>

---------

Co-authored-by: Andrey Sitnik <andrey@sitnik.ru>
@ai
Copy link
Member

ai commented Mar 9, 2023

The fix was released in 10.4.14.

@ai ai closed this as completed Mar 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants