Async vs Defer in JavaScript: Which is Better?🤔

Kai Nguyen

Hatched by Kai Nguyen

Apr 26, 2024

5 min read

0

Async vs Defer in JavaScript: Which is Better?🤔

In the world of web development, optimizing page load times is crucial for providing a seamless user experience. One aspect that can significantly impact load times is how JavaScript files are loaded and executed. Two commonly used attributes, async and defer, offer different approaches to this problem. In this article, we will explore the differences between async and defer and determine which one is better suited for your needs.

When a browser encounters a script tag in an HTML document, it typically halts the parsing of the HTML until the JavaScript file is fetched and executed. This can lead to slower page load times, as the browser waits for the JavaScript file to be downloaded and processed before proceeding with rendering the rest of the page.

The async attribute offers a solution to this problem. By adding the async attribute to a script tag, it tells the browser to download the script asynchronously while parsing the HTML document. Once the script is downloaded, it's executed asynchronously, allowing the browser to continue parsing and rendering the page without waiting for the JavaScript file to finish loading. This can greatly improve page load times, especially for scripts that are not essential for the initial rendering of the page.

On the other hand, the defer attribute provides a similar functionality but with a slight difference in execution timing. Like the async attribute, defer also tells the browser to download the script asynchronously while parsing the HTML document. However, the execution of the script is deferred until after the HTML document has been fully parsed. This ensures that the script does not block the rendering of the page, but it still allows the script to be executed in the order in which it appears in the HTML document.

Now that we have a basic understanding of async and defer, let's dive deeper into the practical introduction to databases.

In the world of databases, there are certain conventions and principles that developers adhere to. For example, it is common practice to put the names of things in lowercase. This helps maintain consistency and readability in the codebase.

The basic element of SQL (Structured Query Language) is the statement or query. SQL is a powerful language that allows us to interact with databases and perform various operations. One of the most common operations is retrieving data from a database using the SELECT statement.

A database is an organized collection of data, managed by a database management system (DBMS). The DBMS is responsible for storing, searching, retrieving, and modifying data in the database. Each table in a database has a name, which usually provides some indication of what kind of data can be found in the table.

The structure of a table is defined by its columns, each of which has a name and an associated data type. The actual data is contained in the rows of the table, with each row representing a single data point and having a value for each column.

To interact with a database, we use query languages like SQL. SQL allows us to express our requests to the DBMS in a declarative manner. It is the most popular query language for relational databases and was developed by engineers at IBM in the 1970s.

Different relational databases may have slightly different implementations of the relational model and may use different "dialects" of SQL. However, the basic principles and syntax remain the same.

SQL statements can be divided into two broad categories: those that change the state of the database (such as inserting, updating, or deleting data), and those that retrieve data from the database.

The SELECT statement is used to retrieve data from a table. The simplest form of the SELECT statement is SELECT * FROM fruit_stand;, which retrieves all data from the table. The * symbol is a shorthand for "all columns in the table".

We can also retrieve specific columns by replacing the * with a comma-separated list of columns: SELECT price, item FROM fruit_stand;. This allows us to specify the columns we wish to retrieve and in what order.

To create a table in SQL, we use the CREATE TABLE statement. This statement allows us to define the structure of the table by listing the columns we want, along with their names and data types. For example, CREATE TABLE fruit_stand (item TEXT, price NUMERIC, unit TEXT); creates a table called fruit_stand with three columns: item, price, and unit.

To add data to a table, we use the INSERT statement. This statement allows us to insert a new row of data into the table. For example, INSERT INTO my_purchase VALUES ('apple', 2, 6.98); inserts a new row into the my_purchase table with the values 'apple', 2, and 6.98 for the item, price, and unit columns, respectively.

When writing SQL statements, there are certain rules and conventions that we should follow. SQL statements should be properly terminated by semicolons, and it is often preferable to write statements on multiple lines for readability. SQL keywords are case-insensitive, so SELECT and select are treated as the same keyword.

Finally, let's discuss some actionable advice for optimizing page load times with JavaScript and improving database interactions with SQL:

  1. Consider using the async attribute for non-essential JavaScript files: If you have JavaScript files that are not crucial for the initial rendering of your page, adding the async attribute can significantly improve page load times. This allows the browser to continue parsing and rendering the page without waiting for the JavaScript file to finish loading.

  2. Use the defer attribute for JavaScript files that depend on the DOM: If you have JavaScript files that need to interact with the DOM, using the defer attribute ensures that the script is executed after the HTML document has been fully parsed. This ensures that the necessary DOM elements are available for the script to work with.

  3. Optimize your SQL queries for efficient data retrieval: When writing SQL queries, consider the performance implications of your statements. Use indexes, limit the number of columns retrieved, and optimize the query structure to minimize the amount of data transferred between the database and the application.

In conclusion, the choice between async and defer in JavaScript depends on the specific requirements of your web page. Async is suitable for non-essential JavaScript files, while defer is more appropriate for scripts that depend on the DOM. By understanding the basics of databases and SQL, you can efficiently retrieve and manipulate data in your applications. Remember to optimize your SQL queries for better performance and always follow best practices when writing JavaScript code.

Sources

← Back to Library

Hatch New Ideas with Glasp AI 🐣

Glasp AI allows you to hatch new ideas based on your curated content. Let's curate and create with Glasp AI :)

Start Hatching 🐣
Async vs Defer in JavaScript: Which is Better?🤔 | Glasp