admin管理员组文章数量:1311486
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.
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.
-
require
is still used when you don't use Babel transpiller. Reason is thatimport
,export
keywords were added to JS in ES6 andasync/await
was added in ES8. This is why you seerequire
d modules in Node.js butimports
in React for ex. – Dominik Matis Commented Feb 3, 2021 at 7:35 -
require
is CommonJS specific syntax that is supported by Node, but not by browsers. I predates ES modules. ES modules are standardized now and supported in more and more environments. – Felix Kling Commented Feb 3, 2021 at 8:20
2 Answers
Reset to default 3They are not the same. I think you should read this:
https://medium./puted-parisons/monjs-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 (mon js modules)
import(x)
allows you to selectively load only the items you need, so it can help save memoryimport(x)
can be run asynchronously, so better performance
本文标签: javascriptDifference between require() and await import()Stack Overflow
版权声明:本文标题:javascript - Difference between require() and await import() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741837142a2400282.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论