BKJang / do-you-know-vanilla

๐ŸŒ This repository contains contents about Vanilla JS and project developed with Vanilla JS.
https://bkjang.github.io/do-you-know-vanilla
11 stars 1 forks source link

repaint์™€ reflow ์ตœ์ ํ™” #10

Open BKJang opened 5 years ago

BKJang commented 5 years ago

๐Ÿ™ Reference

BKJang commented 5 years ago

repaint์™€ reflow ์ตœ์ ํ™”

Repaint์™€ Reflow๊ฐ€ ๋งŽ์•„์งˆ์ˆ˜๋ก ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ Œ๋”๋ง ์„ฑ๋Šฅ์€ ๋Š๋ ค์ง€๊ฒŒ ๋œ๋‹ค. ์ฆ‰, ์ด๋ฅผ ์ค„์ผ์ˆ˜๋ก ์„ฑ๋Šฅ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.

DOM๊ฐ์ฒด์˜ ์บ์‹ฑ

//Before
for(var i=0; i<100; i++) {
    document.getElementById('container').style.padding = i + 'px';
}

//After
var container = document.getElementById('container');

for(var i=0; i<100; i++) {
    container.style.padding = i + 'px';
}

class๋ช…๊ณผ cssText์‚ฌ์šฉ

//Before
var container = document.getElementById('container');

container.style.padding = "20px";
container.style.border = "10px solid red";
container.style.color = "blue";

//After cssText
container.style.cssText = 'padding:20px;border:10px solid red;color:blue;';

//After class
container.className = 'test';

์• ๋‹ˆ๋ฉ”์ด์…˜์ด ๋“ค์–ด๊ฐ„ ๋…ธ๋“œ๋Š” ๊ฐ€๊ธ‰์  position:fixed ๋˜๋Š” position:absolute๋กœ ์ง€์ •

<div id="animation" style="background:blue;position:absolute;"></div>

ํ”„๋ ˆ์ž„์— ๋”ฐ๋ผ reflow๋น„์šฉ์ด ๋งŽ์€ ์• ๋‹ˆ๋ฉ”์ด์…˜ ํšจ๊ณผ์˜ ๊ฒฝ์šฐ์—” ๋…ธ๋“œ์˜ position์„ absolute๋‚˜ fixed๋กœ ์ฃผ๋ฉด ์ „์ฒด ๋…ธ๋“œ์—์„œ ๋ถ„๋ฆฌ๋œ๋‹ค. ์ด ๊ฒฝ์šฐ์—”, ์ „์ฒด ๋…ธ๋“œ์— ๊ฑธ์ณ Reflow ๋น„์šฉ์ด ๋“ค์ง€ ์•Š์œผ๋ฉฐ ํ•ด๋‹น ๋…ธ๋“œ์˜ Repaint ๋น„์šฉ๋งŒ ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.

ํ…Œ์ด๋ธ” ๋ ˆ์ด์•„์›ƒ์„ ํ”ผํ•œ๋‹ค.

ํ…Œ์ด๋ธ”๋กœ ๊ตฌ์„ฑ๋œ ํŽ˜์ด์ง€ ๋ ˆ์ด์•„์›ƒ์˜ ๊ฒฝ์šฐ, ์ ์ง„์  ํŽ˜์ด์ง€ ๋ Œ๋”๋ง์ด ์ผ์–ด๋‚˜์ง€ ์•Š๊ณ  ๋ชจ๋“  ๊ณ„์‚ฐ์ด ์™„๋ฃŒ๋œ ํ›„, ํ™”๋ฉด์— ๋ Œ๋”๋ง์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ”ผํ•˜๋Š”๊ฒŒ ์ข‹๋‹ค.

Virtual DOM์˜ ์‚ฌ์šฉ

Virtual DOM์€ React๋‚˜ Angular์™€ ๊ฐ™์€ UI/UX๊ธฐ๋ฐ˜์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํ˜น์€ ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ์˜ ์ปจ์…ˆ์ด ๋˜๋Š” ๊ฐœ๋…์ด๋‹ค.

๊ธฐ์กด์— javascript๋‚˜ jQuery์—์„œ ์‚ฌ์šฉ๋˜๋˜ DOM ์ง์ ‘์ ‘๊ทผ ๋ฐฉ์‹์˜ ๋ฌธ์ œ๋Š” reflow์™€ repaint์˜ ์—ฐ๊ด€์„ฑ๋„ ๋นผ๋†“์„ ์ˆ˜ ์—†๋‹ค.

DOM์€ ์ •์ ์ด๋‹ค. DOM ์š”์†Œ์— ์ ‘๊ทผํ•˜์—ฌ ๋™์ ์œผ๋กœ ์ด๋ฒคํŠธ๋ฅผ ์ฃผ์–ด layout์„ ๋ฐ”๊พธ๊ฒŒ ๋˜๋ฉด reflow์™€ repaint๊ฐ€ ์ผ์–ด๋‚˜๊ฒŒ ๋œ๋‹ค.

์ด ๊ณผ์ •์—์„œ ๊ทœ๋ชจ๊ฐ€ ํฐ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ผ์ˆ˜๋ก recalculateํ•  ์–‘์ด ๋Š˜์–ด๋‚˜๊ณ  ์ด๋Š” ์„ฑ๋Šฅ์— ํฐ ์˜ํ–ฅ์„ ๋ฏธ์นœ๋‹ค.

  1. ๋ฐ์ดํ„ฐ๊ฐ€ ์—…๋ฐ์ดํŠธ๋˜๋ฉด, ์ „์ฒด UI ๋ฅผ Virtual DOM ์— ๋ฆฌ๋ Œ๋”๋ง.
  2. ์ด์ „ Virtual DOM ์— ์žˆ๋˜ ๋‚ด์šฉ๊ณผ ํ˜„์žฌ์˜ ๋‚ด์šฉ์„ ๋น„๊ต.
  3. ๋ฐ”๋€ ๋ถ€๋ถ„๋งŒ ์‹ค์ œ DOM ์— ์ ์šฉ.

์ฆ‰, Virtual DOM์„ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ๋ฐ”๋€ ๋ถ€๋ถ„(Component)๋งŒ rerenderingํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์—…๋ฐ์ดํŠธ ๋  ๋•Œ, ๋ ˆ์ด์•„์›ƒ ๊ณ„์‚ฐ์ด ํ•œ ๋ฒˆ๋งŒ ์ผ์–ด๋‚˜๊ฒŒ ๋œ๋‹ค.