import * as VS import { ... }

The documentation example uses

import * as FileSystem from 'expo-file-system';

Is it better to import individual items instead, so that asset size is optimized when tree shaking is supported in the future?
Eg.

import { documentDirectory, readAsStringAsync } from 'expo-file-system';

The short answer is yes, that would certainly help. But that wouldn’t do justice to what the Expo team has done recently, let’s dig a bit deeper in this :grin:

Example: Lodash

Take the Lodash library for example. That’s a big one, it includes all the functions they have created. Usually, you only need a couple of them. If you only want to use, let’s say the groupBy method, you can import it with from 'lodash' or from 'lodash/groupBy'.


This is a significant change, ±3.5x - ±4x smaller! Keep in mind that this is only relevant for large libraries like Lodash.

Worth noting that Expo itself uses Lodash with import METHOD from 'lodash/METHOD'; as you can see here. It was also discussed here too.

Example: Expo modules

When Expo wasn’t split up and you had to import it with import Expo from 'expo';, I would consider that a big library. Luckily for us, the Expo team decided that this should be modular for improved flexibility. It means that stuff is now split out into smaller (reusable) packages.

As you can see on Bundlephobia, expo-file-system (9.8kb - 2.8kb), expo-permissions (3.3kb - 1.3kb) and even expo-sensors (4.7kb - 1.5kb) are relatively small packages. Even if tree shaking was enabled, you would have a minimal benefit from this.

To summarize

This is only relevant for larger packages. I don’t think the expo modules qualifies as “large packages”. And because of this, the benefits are really, really marginal at best. Personally, I’d rather keep the readability benefit with FileSystem.documentDirectory() instead of documentDirectory().

If you are concerned about this, I highly recommend the “Import Cost” plugin for vscode or just check with bundlephobia when using packages.

Hope it helps :heart:!

2 Likes

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.