Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does unused import and objects have a performance impact

I have a doubt, whether the unused imports and unused objects in Java code create any performance impact?

Suppose an object is initialized and never used, what happens? And what is the cost of unused imports?

like image 657
Dheeraj Joshi Avatar asked Jan 04 '12 08:01

Dheeraj Joshi


People also ask

Why are unused imports bad?

An unused import still creates a dependency. If you don't realize that a dependency is only because of unused imports, you can waste time updating the module version, investigating vulnerability reports related to that module, etc.

Do unused imports affect performance react native?

Yes, removing these will reduce size but not much.

What does unused import statement mean?

unused import means you imported something that you never used...

Does import make my class bigger?

Import statement doesn't make your class bigger. Why because, importing a class means that to tell the compiler to load the specified class into memory during compilation.


2 Answers

Its a very common question.

Like most performance questions the best approach is to write the clearest and simplest code you can as this improves the maintainability of the code and helps ensure it performs reasonably well even after it is changed. (Clever/Obtuse/Needlessly Verbose code can run fast to start with but as it is changed by mere mortals it can get much slower)

Unused imports have a trivial impact on the compiler, but there are no imports in the byte code or at runtime.

Unused objects can be optimised away, but its best to avoid these as they almost always cause some performance impact, but more importantly make reading and maintaining your code more difficult.

like image 91
Peter Lawrey Avatar answered Sep 30 '22 03:09

Peter Lawrey


Unused imports have no performance impact at runtime. It is purely a namespace mechanism. Nonetheless, you should always import only what you need for readability and avoid namespace collisions which are a nuisance.

Apart from code readability and hence maintainability of code, there may be faster compilation of java code (however, unnoticeable) by tidying up imports, but runtime performance is not impacted, since byte code generated is not impacted by untidy imports. Byte code generated remains the same.

like image 20
Frankline Avatar answered Sep 30 '22 04:09

Frankline