๐Ÿ“– HTML(2) script ํƒœ๊ทธ์˜ ์œ„์น˜

Lee Jooamยท2022๋…„ 4์›” 22์ผ
0

script ํƒœ๊ทธ์™€ ๋”œ๋ ˆ์ด

// index.js

function init() {
  for (let i = 0; i < 1000000; i++) {
    console.log("woo");
  }
}

init();
<!-- index.html -->

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./index.js"></script>
  </head>
  <body>
    <h1>hello world</h1>
  </body>
</html>

์œ„์™€ ๊ฐ™์ด 1,000,000๋ฒˆ์˜ console ์ถœ๋ ฅ์„ ์‹คํ–‰ํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.

์‹ค์ œ๋กœ ์ด index.html ํŒŒ์ผ์„ ์‹คํ–‰์‹œํ‚ค๋ฉด hello world๊ฐ€ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚˜๋Š”๋ฐ ํ•œ์ฐธ์ด๋‚˜ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฐ๋‹ค.

์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ fetchํ•˜๊ณ  executionํ•˜๋Š” ์‹œ๊ฐ„ ๋™์•ˆ HTML์˜ ํŒŒ์‹ฑ์ด ๋ฉˆ์ถ”๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋งŒ์•ฝ ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์—์„œ DOM ์š”์†Œ์— ์ ‘๊ทผํ•œ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ HTML ํŒŒ์‹ฑ์ด ํ•ด๋‹น DOM ์š”์†Œ๊นŒ์ง€ ๋งˆ์น˜์ง€ ์•Š์€ ์ƒํ™ฉ์ด๋ผ๋ฉด ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค.

์ด ํ˜„์ƒ์— ๋Œ€ํ•œ ์ฒซ ๋ฒˆ์งธ ํ•ด๊ฒฐ์ฑ…์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

script ํƒœ๊ทธ๋ฅผ body ์ตœํ•˜๋‹จ์— ๋„ฃ๊ธฐ

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1>hello world</h1>
    <script src="./index.js"></script>
  </body>
</html>

๋‹ค์Œ๊ณผ ๊ฐ™์ด script ํƒœ๊ทธ๋ฅผ body์˜ ๋งจ ๋ฐ‘ ๋ถ€๋ถ„์— ์„ ์–ธํ•˜๋Š” ๊ฑด ํ”ํ•œ ์ƒํ™ฉ์ด๋‹ค.

์Šคํฌ๋ฆฝํŠธ ํƒœ๊ทธ๋ฅผ ์ตœํ•˜๋‹จ์œผ๋กœ ๋‚ด๋ ค HTML ๋ฌธ์„œ๊ฐ€ ๋จผ์ € ๋ Œ๋”๋ง ๋˜๋„๋กํ•œ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•œ๋‹ค๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ ๋ฐ›๋Š” ๋กœ๋”ฉ์— ๋Œ€ํ•œ ์ฒด๊ฐ์„ ๋‚ฎ์ถœ ์ˆ˜ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ด๊ฒƒ์€ ์™„๋ฒฝํ•œ ํ•ด๊ฒฐ์ฑ…์ด ์•„๋‹ˆ๋‹ค.

์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์˜ ์‹คํ–‰์ด ์•„์ฃผ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๊ณ , ๋งŽ์€ ์ด๋ฒคํŠธ๋“ค์ด ๊ทธ ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์— ์ž‘์„ฑ๋˜์–ด ์žˆ๋‹ค๋ฉด ์‚ฌ์šฉ์ž์™€ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐ„์˜ ์ƒํ˜ธ์ž‘์šฉ์— ๋”œ๋ ˆ์ด๊ฐ€ ์ƒ๊ธฐ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๋˜ํ•œ HTML ํŒŒ์ผ์ด ์•„์ฃผ ํด ๋•Œ๋„ script์˜ ์‹คํ–‰์— ์˜ค๋žœ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์™„๋ฒฝํ•˜๊ฒŒ ํŽ˜์ด์ง€๊ฐ€ ๋กœ๋”ฉ๋  ๋•Œ๊นŒ์ง€ ๋”œ๋ ˆ์ด๊ฐ€ ์ƒ๊ธฐ๊ฒŒ ๋œ๋‹ค.

์ด๋Ÿฐ ์ƒํ™ฉ์ด ๋ชจ๋˜ํ•œ ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” ์–ด๋–ค ์‹์œผ๋กœ ์ฒ˜๋ฆฌ๋˜๋Š” ์ง€ ์•Œ์•„๋ณด์ž.

defer ์†์„ฑ

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script defer src="./index.js"></script>
    <title>Document</title>
  </head>
  <body>
    <h1>hello world</h1>
  </body>
</html>

์™ธ๋ถ€์—์„œ ๋ถˆ๋Ÿฌ์˜จ ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์€ fetch(๋ถˆ๋Ÿฌ์˜ค๊ธฐ)์™€ execution(์‹คํ–‰) ๊ณผ์ •์„ ๊ฑฐ์นœ๋‹ค.

defer ์˜ต์…˜์€ ์ด ๋‘ ๊ณผ์ • ์ค‘ fetch ๊ณผ์ •์„ HTML ํŒŒ์‹ฑ๊ณผ ํ•จ๊ป˜ ํ•˜๋„๋ก ํ•ด์ค€๋‹ค. fetch๋ฅผ ๋งˆ์นœ script ํŒŒ์ผ์€ ํŽ˜์ด์ง€์˜ ๊ตฌ์„ฑ์ด ๋๋‚˜๊ณ  ๋‚œ ํ›„ ์‹คํ–‰๋œ๋‹ค.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script defer src="./index.js"></script>
    <script defer src="./index_long.js"></script>
    <script defer src="./index_small.js"></script>

    <title>Document</title>
  </head>
  <body>
    <h1>hello world</h1>
  </body>
</html>

defer ์˜ต์…˜์„ ๊ฐ€์ง„ ์Šคํฌ๋ฆฝํŠธ ํƒœ๊ทธ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ? ๋ชจ๋“  ํŒŒ์ผ์€ script ํƒœ๊ทธ๋ฅผ ๋งŒ๋‚˜ ๋ฉˆ์ถ”์ง€ ์•Š๊ณ  fetch ๊ณผ์ •์„ ์ง„ํ–‰ํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ํŽ˜์ด์ง€์˜ ๊ตฌ์„ฑ์ด ๋๋‚˜๋ฉด ๊ฐ๊ฐ ํŒŒ์ผ์„ ์‹คํ–‰ํ•œ๋‹ค. ์ด๋•Œ ์‹คํ–‰๋˜๋Š” ์ˆœ์„œ๋Š” ๋จผ์ € fetch๋œ ์ˆœ์„œ๊ฐ€ ์•„๋‹Œ ํŒŒ์ผ์ด ๋“ฑ๋ก๋œ ์ˆœ์„œ์ด๋‹ค.

async ์†์„ฑ

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script async src="./index.js"></script>
    <script async src="./index_long.js"></script>
    <script async src="./index_small.js"></script>

    <title>Document</title>
  </head>
  <body>
    <h1>hello world</h1>
  </body>
</html>

async ์˜ต์…˜์€ defer์™€ ์œ ์‚ฌํ•˜๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ script ํƒœ๊ทธ๋ฅผ ๋งŒ๋‚˜๋Š” ์ฆ‰์‹œ HTML ํŒŒ์ผ์˜ ํŒŒ์‹ฑ์„ ๋ฉˆ์ถ”์ง€ ์•Š๊ณ  fetch๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.

ํ•˜์ง€๋งŒ defer๋Š” ๋ชจ๋“  ํŽ˜์ด์ง€ ๊ตฌ์„ฑ์ด ๋๋‚˜๊ณ  ๋‚œ ๋’ค ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์‹คํ–‰๋˜๋Š” ๊ฒƒ๊ณผ ๋‹ฌ๋ฆฌ async ์†์„ฑ์€ ์Šคํฌ๋ฆฝํŠธ์˜ fetch๊ฐ€ ๋๋‚œ ์ฆ‰์‹œ ์‹คํ–‰๋œ๋‹ค.

์ด๋Ÿฐ ํŠน์„ฑ ๋•๋ถ„์— ๋…๋ฆฝ์ ์œผ๋กœ ์ž‘๋™ํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ๋ผ๋ฉด ๋ณ‘๋ ฌ์ ์œผ๋กœ ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌ๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์ง€๋งŒ, ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ ์ˆœ์„œ๋‚˜ ํŠน์ • DOM ์š”์†Œ์— ์˜์กด์ ์ด๋ผ๋ฉด ์ฃผ์˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

ํ›„๊ธฐ

์ถœ์ฒ˜: https://www.webtips.dev/webtips/javascript-interview/async-and-defer-in-javascript

๊ฐ„๋‹จํ•˜๊ฒŒ๋Š” ๊ทธ๋ƒฅ ๋ชจ๋“  script ํƒœ๊ทธ์— defer ์†์„ฑ์„ ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค. ํ•˜์ง€๋งŒ ์‚ฌ์šฉ์ž์™€ ์ง์ ‘์ ์œผ๋กœ ๋งŒ๋‚˜๋Š” ํ”„๋ก ํŠธ์—”๋“œ์ธ๋งŒํผ ์ตœ์ ํ™”์— ์‹ ๊ฒฝ์จ์•ผ ๋œ๋‹ค.

์ตœ์ ์˜ ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ์œ„ํ•ด์„  ๋…๋ฆฝ์ ์ธ ์Šคํฌ๋ฆฝํŠธ๋Š” async๋กœ, ์˜์กด์„ฑ์ด ๋†’์€ ์Šคํฌ๋ฆฝํŠธ๋Š” defer ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒŒ ๋ฐ”๋žŒ์งํ•˜๋‹ค.

๋ฌผ๋ก  ๊ทธ ์ด์ „์— ๋‘ ์†์„ฑ์„ ์ ์žฌ์ ์†Œ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก js ํŒŒ์ผ์„ ์„ค๊ณ„ํ•˜๋Š” ๊ฒŒ ์šฐ์„ ์ด๊ฒ ์ง€๋งŒ ๋ง์ด๋‹ค.

์ฐธ๊ณ ๋กœ ๋‘ ์˜ต์…˜์€ ์™ธ๋ถ€ ์Šคํฌ๋ฆฝํŠธ์—์„œ๋งŒ ์œ ํšจํ•˜๋‹ค.

profile
ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž๋กœ ๊ฑธ์–ด๊ฐ€๋Š” ์ค‘์ž…๋‹ˆ๋‹ค.

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