The advantages and disadvantages of AMD, CMD, CommonJs, the difference?

To understand what is AMD, CMD, CommonJs? You also need to understand the modularity of Js

What is modularity?

Package a complex program into several files according to certain rules, and combine them together. The internal data of each module is private, and only some interfaces (methods) are exposed to the outside to communicate with other external modules.

Advantages of modularity?

1. Avoid namespace conflicts (reduce namespace pollution)
2. Better separation, realize on-demand loading
3. Improve code reusability
4. Improve code maintainability
The history of modular development

The way to achieve modularity

insert image description here
1. AMD/CMD/CommonJs is the standard for JS modular development. The current corresponding implementation is RequireJs/SeaJs/nodeJs
2. CommonJs is mainly aimed at the server side, and AMD/CMD is mainly aimed at the browser side.
3. The difference between AMD/CMD, although both It is to load js files in parallel, but there is still a difference. AMD is preloading. While loading js files in parallel, it will also parse and execute the module (because it still needs to be executed, so before loading a module, the dependent modules of this module need The loading is completed first); and CMD is lazy loading, although it will load the js file in parallel at the beginning, but it will not be executed, but will be executed when it is needed.

Advantages and disadvantages of AMD and CMD

Advantages of AMD: Fast loading, especially when encountering multiple large files. Because of parallel parsing, multiple files can be parsed at the same time.
Disadvantages of AMD: Parallel loading, asynchronous processing, and the loading order is not certain, which may cause some troubles, and even bury a big hole for the program.

Advantages of CMD: Because js files are only parsed and executed when they are used, the execution sequence of each JS file is reflected in the code and is controllable.
Disadvantages of CMD: The execution waiting time will be superimposed. Because each file is executed synchronously (serial execution), the time is the sum of the parsing and execution time of all files, especially when there are many large files, this shortcoming is particularly obvious.

Guess you like

Origin blog.csdn.net/u013994400/article/details/126832927