Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between require() and await import()

I would like to know the difference between require(x) and await import(x) in terms of code splitting and lazy loading. Are they both the same? If yes, then why does await import(x) exist in the first place as one can use require() statements anywhere he wants. Any in depth answer would be highly appreciated.

like image 568
KMA Badshah Avatar asked May 01 '26 10:05

KMA Badshah


2 Answers

They are not the same. I think you should read this:

https://medium.com/computed-comparisons/commonjs-vs-amd-vs-requirejs-vs-es6-modules-2e814b114a0b

import is when requiring an es module, that's the new ecma script standard, it has many benefits over require (common js modules)

like image 193
misha1109 Avatar answered May 02 '26 23:05

misha1109


  1. import(x) allows you to selectively load only the items you need, so it can help save memory
  2. import(x) can be run asynchronously, so better performance
like image 20
RuiSiang Avatar answered May 02 '26 22:05

RuiSiang