After running a script that checks for unused imports
such as import XYZ from 'dir/XYZ.jsx'
where XYZ
is never used, I've come across about 300 files that have this sort of issue. Most of these files are missing similar things such as Proptypes
for React (import PropTypes from 'prop-types'
).
I heard that webpack/babel cache files after they are imported. If this is true, would it be worth it to remove these unused imports that occur many times, or should I just leave them alone?
Note that I'm doing this project for performance reasons and also for code cleanliness.
You already answered to your own questions:
Note that I'm doing this project for performance reasons and also for code cleanliness.
However, let's we break the answer, with an example scenario and some use-cases.
Scenario:
Lets say we have a Forecast app. When the user go to the daily forecast route, then he wants to get info only about the current day.
Use cases:
weeklyForecast
utility, in the DailyComponent
? My point here is it's misleading and confusing, unless you review the whole DailyComponent
. Maybe someone will say that we have Linters for that. But why do you use Linter, if you don't follow its rules and conventions?Technically speaking, it's a performance loss (use-case 1) if you import something, that's never used, but served to the user. I'll recommend you to check webpack optimization practices (Minimize, Deduplication, Chunks).
Also do you know about Webpack Code Splitting?
Code splitting is one of the most compelling features of webpack. This feature allows you to split your code into various bundles which can then be loaded on demand or in parallel. It can be used to achieve smaller bundles and control resource load prioritization which, if used correctly, can have a major impact on load time.
Now imagine if you do code splitting in your app, but you still have many unused imports. Тhat will increase the initial app load time and will violate the code splitting idea.
Conclusion:
For new project, obviously there aren't any benefits to include unused imports.
According to your case, you can measure what's the load time of specific routes with and without the unused imports. Most of the IDEs offers this feature for removing unused imports, therefore you can calculate the impact.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With