async, defer

Robinยท2023๋…„ 1์›” 3์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
22/24
post-custom-banner

without async, defer

<!-- 1. header์— ๋„ฃ๊ธฐ -->
<head>
	<script src="main.js"></script>
</head>

<!-- 2. body ๋งˆ์ง€๋ง‰์— ๋„ฃ๊ธฐ -->
<body>
	<div></div>
	<script src="main.js"></script>
</body>
  • html parsing ๐Ÿ‘‰ js fetching ๐Ÿ‘‰ js executing

head + async

<head>
	<script async src="main.js"></script>
</head>
  • async ์ž์ฒด๊ฐ€ boolean ๊ฐ’์œผ๋กœ, html parsing๊ณผ js fetching์„ ๋ณ‘๋ ฌ์ ์œผ๋กœ ์‹คํ–‰
  • async ๋‹จ์ 
    1. js๊ฐ€ ์กฐ์ž‘ํ•˜๋ ค๊ณ  ํ•˜๋Š” ์š”์†Œ(html)๊ฐ€ ์ •์˜ ๋˜์–ด ์žˆ์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค.
    2. ์—ฌ์ „ํžˆ executing ์‹œ๊ฐ„ ๋™์•ˆ ๋ฉˆ์ถ”๋‹ค๋ณด๋‹ˆ ์‚ฌ์šฉ์ž๊ฐ€ ํŽ˜์ด์ง€๋ฅผ ๋ณด๋Š”๋ฐ์— ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ์ˆ˜ ์žˆ๋‹ค.
    3. ์—ฌ๋Ÿฌ๊ฐœ์˜ js ํŒŒ์ผ์„ script๋กœ ๋ถˆ๋Ÿฌ์˜ฌ ์‹œ, async๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŒจ์นญ๋˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰์‹œํ‚จ๋‹ค.
      ์ฆ‰ ์ •์˜๋œ ์ˆœ์„œ์™€ ์ƒ๊ด€์—†์ด ๋‹ค์šด๋กœ๋“œ ๋œ ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰๋˜๊ธฐ์— ๋งŒ์•ฝ js๊ฐ€ ์ˆœ์„œ์— ์˜์กด์ ์ด๋ผ๋ฉด ์ ํ•ฉํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.
    <head>
    	<script async src="a.js"></script>
    	<script async src="b.js"></script>
    	<script async src="c.js"></script>
    </head>

head + defer

<head>
	<script defer src="main.js"></script>
</head>
  • js fetching๊ณผ html parsing ๋ณ‘๋ ฌ์ ์œผ๋กœ ์‹คํ–‰
  • html parsing์ด ๋‹ค ๋๋‚œ ์ดํ›„์— js executing
  • js fetching html parsing ์™„๋ฃŒ ํ›„ ๐Ÿ‘‰ js executing
    ์šฐ๋ฆฌ๊ฐ€ ์ •์˜ํ•œ js ํŒŒ์ผ ์ˆœ์„œ(์œ„์˜ ์˜ˆ์‹œ์˜ ๊ฒฝ์šฐ a > b > c)๊ฐ€ ์ง€์ผœ์ง„๋‹ค.

์ฐธ๊ณ : ๋“œ๋ฆผ์—˜๋ฆฌ์ฝ”๋”ฉ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ์ดˆ๊ฐ•์˜

profile
Always coding or dog walking
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€