search

Home  >  Q&A  >  body text

javascript - understanding of asynchronous modules

For synchronization modules, the module must be loaded into memory in advance.
For asynchronous modules, for example, its implementation of requirejs relies on front-end and is executed in advance. Isn't this also loaded into memory in advance? Then why is it said to be asynchronous? After reading a lot about AMD and CMD, I just don’t quite understand the difference between synchronous module loading and asynchronous module loading.

曾经蜡笔没有小新曾经蜡笔没有小新2700 days ago997

reply all(2)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-07-05 10:51:39

    This mechanism has little to do with [memory], and can be understood more as [script download timing].

    The particularity of the front-end is that the size and download time of the script file will directly affect the page performance. In the specification of synchronous loading, all dependencies must be downloaded before the script can be executed.

    In the specification of asynchronous loading, the required script will be loaded only when the code runs to the required location. For example, in a huge single-page application, the script on the home page can be very light, and when entering a certain function page, the required modules can be loaded asynchronously, which will greatly help the performance.

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-07-05 10:51:39

    The asynchronous loading of requirejs essentially appends the <script> that needs to be loaded asynchronously in the <head>. These <script> elements have the async attribute to achieve non-blocking asynchronous loading. Then monitor the loading completion status of each script through onload to realize the sequential loading order of multiple scripts with dependencies. You can use async and onload to implement simple asynchronous loading functions, but it still takes a lot of effort to implement functions like requirejs.

    reply
    0
  • Cancelreply