What are the advantages of using async JavaScript modules (like AMD) for traditional sites?

This is specific to JS in the browser for a traditional multi-page site (i.e. not a single-page app).

For single-page apps, the advantages are pretty clear: the main view page is going to be long-lived, and it will load any number of sub-views which may all have different JS dependencies. Loading all your JS up front is not ideal for something of non-trivial size, so having explicit, structured dependency declarations and using an async script loader seems like the way to go.

However for a more traditional, multi-page design, where each page is a full, separate HTML page, I’m not seeing the benefit. In this design all your JS dependencies are known at page load. So ideally you’d just put those all together into a single script on the server side and download it in one go – loading separate modules with separate requests would be bad for performance.

So when you guarantee that all needed JS modules are there on page load, I don’t see the need for something like AMD. You can use the module pattern to create modules – you can use simple objects to create namespaces. Is there something that I’m missing?

For single-page apps, the advantages are pretty clear … having explicit, structured dependency declarations and using an async script loader seems like the way to go.

Those aren’t the only advantages, though. AMD-style modules don’t just remove the need for namespaces, they provide other benefits, like eliminating hard-coded dependencies and facilitating dependency injection for mocking/testing. This question goes into more detail.

However for a more traditional, multi-page design … you’d just put those all together into a single script on the server side and download it in one go – loading separate modules with separate requests would be bad for performance.

A source tree of AMD-style modules can easily be compiled into a single resource with a tool like RequireJS Optimizer. This gives you the best of both worlds; while developing and testing you can load the modules separately, and get meaningful debugging information without resorting to source maps. You can make changes to a script and test it without rebuilding your project. In production, you can compile the modules into a single resource.

So when you guarantee that all needed JS modules are there on page load, I don’t see the need for something like AMD.

How do you guarantee that all needed modules are there on page load? If you use AMD modules, each module knows its own dependencies and you can point a tool like RequireJS Optimizer at a single entrypoint module and be done with it. You could also reuse the same modules in other apps (including single-page apps) without changing anything; the modules can be loaded individually or combined into single resources.

If you go the namespace route, you’ll need to track which modules are needed for each page, manage the entire dependency graph for each entrypoint module, and manage the compilation process yourself somehow.

In other words, the script loader is not the only benefit of AMD. The other major benefit is that each module is responsible for its own dependencies (rather than just assuming that some namespaces exist, and leaving it up to the developer to ensure that the required namespaces are set up before the module can do its thing).

1

The difference you make between single-page applications and multi-page sites may not be particularly relevant today. Most web apps are between those two: they have multiple pages, a common JavaScript code which is used on every or nearly every page, and some code used on specific pages only.

This makes your suggestion of serving a single minified JavaScript file per page less practical. You’ll end up with large files, and different files for every page, resulting in poor performance and increased bandwidth usage.

From here, there are three generic solutions:

  1. Just throwing all JavaScript into one big minified file which is called from every page of the web app.

    This works well in most cases, but would become problematic if there is too much JavaScript or in some specific situations, such as a case where the home page doesn’t contain a lot of interactivity and is expected to be extremely fast.

  2. Having one big minified file of common code called on every page and dedicated code on specific pages.

    This is an improvement when performance becomes an issue, but increases maintenance complexity. If the architecture is poor and/or inexperienced programmers are working on JavaScript code, this may quickly result in a mess.

  3. Using require.js to load JavaScript on-demand. Here, all the complexity is handled by require.js, which means that developers can achieve performance similar to the second solution while focusing on the code rather than on the organization.

So to answer your question, using something like AMD simply allows to mitigate some of the complexity from developers while keeping performance footprint low.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật