Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why shouldn't you use import all in ES6

So I recently started learning react and noticed that all the documentation has imports that look like else:

import { Apples, Bananas, Oranges } from 'fruits';

But while studying react I found that this syntax works just as well:

import * as Fruits from 'fruits';

My Question: Is there any performance loss or logical conflict with using the import all syntax?

If there is not then I will just keep using that syntax. I would rather be a little more verbose and not have to worry about making sure everything has been imported.

like image 833
Nicholas Summers Avatar asked Nov 14 '17 20:11

Nicholas Summers


People also ask

Does ES6 support import?

Introduction to ES6 import:The import statement is used to import modules that are exported by some other module. A module is a file that contains a piece of reusable code. The import modules are in strict mode whether it is declared or not.

Is it better to use require or import?

One of the major differences between require() and import() is that require() can be called from anywhere inside the program whereas import() cannot be called conditionally, it always runs at the beginning of the file. To use the require() statement, a module must be saved with . js extension as opposed to .

Why we use curly braces to import in JavaScript?

Curly braces { } are special syntax in JSX. It is used to evaluate a JavaScript expression during compilation. A JavaScript expression can be a variable, function, an object, or any code that resolves into a value.

Does ES6 import async?

ES6 module loaders will be asynchronous while node. js module loaders are not.


1 Answers

It would be better to use the first way. At least for one thing: Always write explicitly what you want to use. It's a best practice in all framework / language.

If you have some tree-shaking, some unused module won't be loaded and everything should be good (like @zerkms said). But it's in the best world case, even the best tree shaking isn't perfect and you could have some import still being imported even if you don't use them. If your project is 'small' it should be ok. If in your project you load hundreds stuff, it could be a little bit different.

And when you will build your project, you will loose time with the tree-shaking analysis too.

So just because you don't want to "loose time by writing two words" you will loose time on every build and maybe have a performance impact

like image 136
sheplu Avatar answered Sep 28 '22 03:09

sheplu