Fast JavaScript?: Dynamic imports

Fast JavaScript: Dynamic imports

For having a clean code in JavaScript, you must use multiple modules which are imported from one to another. But some of the functions are not required exactly when you import them. Some of them will not see the light of the browser because, in some cases, the information doesn't fulfill the condition for them to be called.

That's why JavaScript created dynamic imports. With it, you can import a function exactly when it has to be called. This way you will improve the app performance and reduce the memory usage.

So, let's see how dynamic imports looks like.

No alt text provided for this image

We have this lines of code. A user which contain a name and a role, based on the role, we call the setCookie function or not.

Because, we may not need to call setCookie function, we can use the dynamic imports. This way, it will be imported only if the user role is "visitor".

The code will look like this:

No alt text provided for this image

In this case, the import is on the if block. This way, if the setCookie will not be needed, the module will not download it and that's how the dynamic imports improve your performance of the code.

Since the dynamic import return is a promise, we can also use the async function, but this is the only place where I prefer to use the then block instead of async/await. But this is my opinion. :)

Let's say that we have an onClick event, and because its function is huge and can be splitted in three, based on the role of the user, we created three modules that contain: handleClickVisitator, handleClickContentCreator and handleClickAdmin.

I know this is not the best case or the best approach without dynamic imports, but focus on the idea. :))

The code would look like this:

No alt text provided for this image

In this example, we will inevitably import and download all 3 modules, even if we only use one of the function and this way.

Now, let's check the case where we use the dynamic import.

No alt text provided for this image

First at all, we can see how clean the code is and that we can now rename all the functions from the modules handleClick, which is better in my opinion. :))

But this is nothing compared with the improvement of the performance and the decreased memory usage.

Please tell me what do you think about dynamic imports and other cases where you think it can be applied for better performance. :)

Thanks for reading my article!

Leave a like if you find this useful, so others like you can benefit from it.

You can also find my articles on my website: mariusatasiei.com/blog


Sources: YouTube and Mozilla

Suraj Kumar

Senior Software Engineer at DocuSign

3 年

Very nice article. But what I would prefer is doing code splitting on module (one product functionality) basis. Lazy loading the modules when needed. But, I will not go for dynamic imports just for 2-3 files. This is my opinion and I might be wrong. Even if your function is expensive (let’s say setcookie in your case). But you are not going to execute that expensive function until it meets the condition (user.role)

Talha fayyaz

Full stack | Langchain | Ai integrations | Ex-Systems | Ex-Techverx |React | React native | Next js | Node | Express | Graphql | Apollo | Storybook | Github Actions | AWS | Jenkins | Serverless | Remote ??

3 年

Thanks for posting

Vineet G Nair

Technical Lead | Scrum Alliance Certified Scrum Master

3 年

This for sure is an good addition to JS.

要查看或添加评论,请登录

Marius Atasiei的更多文章

  • Boost Your Flutter App's Speed with Dart?Isolates

    Boost Your Flutter App's Speed with Dart?Isolates

    Imagine building a super-fast Flutter app that never leaves your users waiting. That’s where Dart isolates come in!…

  • Effortless App Development: Flutter and Riverpod in Action

    Effortless App Development: Flutter and Riverpod in Action

    Flutter, Google’s UI toolkit, has revolutionized how developers build beautiful, cross-platform apps. When paired with…

  • SOLID Principles using Dart

    SOLID Principles using Dart

    In the world of software development, crafting maintainable, scalable, and adaptable code is crucial. SOLID principles…

  • CSS Tips: 3 Features that you should know

    CSS Tips: 3 Features that you should know

    1) is function If you have to target multiple elements from multiple targets like this: You can use the is function to…

    1 条评论
  • Clean React: Better Imports

    Clean React: Better Imports

    JavaScript may be the worst programming language when it comes to import. And we all hate those 20 lines of imports we…

    2 条评论
  • 8 VSCODE EXTENSIONS THAT EVERY WEB DEVELOPER HAVE TO USE

    8 VSCODE EXTENSIONS THAT EVERY WEB DEVELOPER HAVE TO USE

    One of the best features that IDEs have, is the extensions. The extensions help you to be as efficient as possible and…

    5 条评论
  • 5 HTML Tips for Better Websites

    5 HTML Tips for Better Websites

    React, Angular, Vue. Indifferent what framework do you use, as a frontend developer, you have to deal with HTML.

  • Advanced TypeScript Methods

    Advanced TypeScript Methods

    Restrict types of parameter dynamically 1. Create type Row and Col 2.

  • GIVE TYPESCRIPT A CHANCE

    GIVE TYPESCRIPT A CHANCE

    TypeScript is a "programming language" which offers you all JavaScript benefits and some more addons. A few things that…