Optimize JavaScript execution

wherehowsยท2022๋…„ 4์›” 2์ผ
1

๐Ÿ“Œ๋ณธ ๋ฌธ์„œ๋Š” Optimize JavaScript execution์„ ๋ฒˆ์—ญํ•œ ๋ฌธ์„œ์ด๋ฉฐ ์˜ค์—ญ, ์˜์—ญ์ด ์กด์žฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์™ธ์— ์ถ”๊ฐ€์ ์œผ๋กœ ์ฐธ๊ณ ํ•œ Reference๋Š” ๋ฌธ์„œ ๋งˆ์ง€๋ง‰์— ๊ธฐ์žฌํ•ด ๋†“์•˜์Šต๋‹ˆ๋‹ค.

์„œ๋ก 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” Style์„ ์ง์ ‘ ์กฐ์ž‘ํ•˜๊ฑฐ๋‚˜, ๋ฐ์ดํ„ฐ ์ •๋ ฌ ํ˜น์€ ๊ฒ€์ƒ‰๋“ฑ์˜ ์ž‘์—…์„ ํ†ตํ•ด์„œ ์‹œ๊ฐ์ ์ธ ์—…๋ฐ์ดํŠธ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์‹คํ–‰ ์‹œ๊ฐ„์ด ๊ธธ๊ฑฐ๋‚˜ ํ˜น์€ ์‹คํ–‰๋˜๋Š” ํƒ€์ด๋ฐ์ด ์•ˆ์ข‹์€ ๊ฒฝ์šฐ ํผํฌ๋จผ์Šค ์ด์Šˆ๋กœ ์ด์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ๊ฐ€๋Šฅํ•œํ•œ ์ด ์˜ํ–ฅ์„ ์ตœ์†Œํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋Š” ์‹ค์ œ ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ์™€๋Š” ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์—, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์„ฑ๋Šฅ์„ ๋ถ„์„ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์˜ˆ์ˆ ์— ๊ฐ€๊นŒ์šด ํ–‰์œ„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋˜ ๋ธŒ๋ผ์šฐ์ €๋Š” JIT ์ปดํŒŒ์ผ๋Ÿฌ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์—ฌ๋Ÿฌ ์ตœ์ ํ™”์™€ ํŠธ๋ฆญ์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ฝ”๋“œ์˜ ๋™์ž‘์„ ํฌ๊ฒŒ ๋ฐ”๊ฟ”๋†“๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๊ทธ๋Ÿผ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ข€ ๋” ์ž˜ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋„๋ก ๋•๋Š” ๋ช‡ ๊ฐ€์ง€ ๋ถ„๋ช…ํ•œ ๋ฐฉ๋ฒ•์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

  1. ์‹œ๊ฐ์ ์ธ ์—…๋ฐ์ดํŠธ๋Š” setTimeout, setInterval ๋Œ€์‹  rAF๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.
  2. ์‹คํ–‰ ์‹œ๊ฐ„์ด ๊ธด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” main thread๊ฐ€ ์•„๋‹Œ Web Worker์—์„œ ์‹คํ–‰์‹œํ‚ต๋‹ˆ๋‹ค.
  3. ์—ฌ๋Ÿฌ ํ”„๋ ˆ์ž„์— ๊ฑธ์ณ์„œ DOM ๋ณ€ํ™”๊ฐ€ ์ผ์–ด๋‚œ๋‹ค๋ฉด, micro-task๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.

๐Ÿค” 1๋ฒˆํ•˜๊ณ  ๊ฐ™์€ ์ด์•ผ๊ธฐ๊ฐ™์Šต๋‹ˆ๋‹ค. ์•„๋งˆ rAF์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” micro-task queue์— enqueue๊ฐ€ ๋˜๊ณ , ์—ฌ๋Ÿฌ ํ”„๋ ˆ์ž„์— ๊ฑธ์ณ์„œ DOM ๋ณ€ํ™”๊ฐ€ ์ผ์–ด๋‚˜๋Š” ๊ฒฝ์šฐ๋Š” animation ์ƒํ™ฉ์„ ์ด์•ผ๊ธฐํ•˜๋Š” ๋“ฏ ํ•ฉ๋‹ˆ๋‹ค. rAF๋ฅผ ์‚ฌ์šฉํ•˜๋ผ๋Š” ์˜๋ฏธ๊ฒ ์ฃ ?

  1. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์˜ํ–ฅ ๋ถ„์„์„ ์œ„ํ•ด์„œ Chrome์˜ DevTool๊ณผ Javascript Profiler๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.

1. ์‹œ๊ฐ์ ์ธ ์—…๋ฐ์ดํŠธ๋Š” setTimeout, setInterval ๋Œ€์‹  rAF๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.

rAF๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ”„๋ ˆ์ž„์˜ ์‹œ์ž‘ ์ง€์ ์—์„œ ํ˜ธ์ถœํ•˜์—ฌ Frame loss๋ฅผ ์ตœ์†Œํ™”ํ•ฉ๋‹ˆ๋‹ค.

/**
    * If run as a requestAnimationFrame callback, this
    * will be run at the start of the frame.
    */
function updateScreen(time) {
    // Make visual updates here.
}

requestAnimationFrame(updateScreen);

setTimeout์ด๋‚˜ setInterval์„ ์‚ฌ์šฉํ•˜์—ฌ ์‹œ๊ฐ์ ์ธ ์—…๋ฐ์ดํŠธ๋ฅผ ํ•˜๋ คํ•˜๋Š” ๊ฒฝ์šฐ, ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ํ”„๋ ˆ์ž„ ๊ฐ„๊ฒฉ์˜ ๋žœ๋คํ•œ ์ง€์ ์—์„œ ํ˜ธ์ถœ๋˜๋ฉฐ, ์ตœ์•…์˜ ๊ฒฝ์šฐ, ๋‹ค์Œ ํ”„๋ ˆ์ž„์ด ๋ณด์—ฌ์ง€๊ธฐ ๋ฐ”๋กœ ์ง์ „์— ํ˜ธ์ถœ๋˜์–ด Frame Loss๋กœ ์ด์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

jQuery๋„ ํ•œ๋•Œ๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์œ„ํ•ด setTimeout์„ ์‚ฌ์šฉํ–ˆ์œผ๋‚˜, version3์— ์˜ค๋ฉด์„œ rAF๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์˜›๋‚  ๋ฒ„์ „์˜ jQuery๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, rAF๋ฅผ ์‚ฌ์šฉํ•˜๋Š” version3์œผ๋กœ์˜ ์—…๋ฐ์ดํŠธ๋ฅผ ๊ฐ•๋ ฅํ•˜๊ฒŒ ๊ถŒ๊ณ ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

๐Ÿค” setTimeout์ด๋‚˜ setInterval์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ๋ชจ๋‹ˆํ„ฐ ์ฃผ์‚ฌ์œจ๋ณด๋‹ค ๋” ๋น ๋ฅด๊ฒŒ ์‹คํ–‰์ด ๋œ๋‹ค๋ฉด, ์ด ์—ญ์‹œ๋„ Frame Loss๋กœ ์ด์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€๋ น 16.7ms์˜ Frame ๊ฐ„๊ฒฉ์„ ๊ฐ–๋Š” ๋ชจ๋‹ˆํ„ฐ์—์„œ, 4.7ms๋กœ setTimeout์„ ํ˜ธ์ถœํ•˜๋Š” ๊ฒฝ์šฐ, 4๋ฒˆ์ด ํ˜ธ์ถœ๋˜์–ด๋„ 3๋ฒˆ์€ Loss๋กœ ์ด์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ rAF์˜ ๊ฒฝ์šฐ, ํ˜ธ์ถœ ์‹œ๊ฐ„์ด ๋ชจ๋‹ˆํ„ฐ ์ฃผ์‚ฌ์œจ์— ์˜ํ•ด ๊ฒฐ์ •๋˜๊ธฐ ๋•Œ๋ฌธ์— ์œ„์™€ ๊ฐ™์€ Frame Loss๋Š” ๊ฑฑ์ •ํ•˜์ง€ ์•Š์•„๋„ ๋˜๋ฉฐ, repaint ์ด์ „์— ํ˜ธ์ถœ๋˜๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•ฉ๋‹ˆ๋‹ค.

2. ์‹คํ–‰ ์‹œ๊ฐ„์ด ๊ธด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” main thread๊ฐ€ ์•„๋‹Œ Web Worker์—์„œ ์‹คํ–‰์‹œํ‚ต๋‹ˆ๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๋ธŒ๋ผ์šฐ์ €์˜ main thread์—์„œ ์‹คํ–‰๋˜๋ฉฐ, ์Šคํƒ€์ผ ๊ณ„์‚ฐ, ๋ ˆ์ด์•„์›ƒ, ํŽ˜์ธํŠธ ๋“ฑ์ด ์ฐจ๋ก€๋Œ€๋กœ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ๊ธธ๋‹ค๋ฉด, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ดํ›„์˜ ์ž‘์—…๋“ค์ด ์ฐจ๋ก€๋Œ€๋กœ block์ด ๋˜์–ด frame loss๋กœ ์ด์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์–ธ์ œ, ๊ทธ๋ฆฌ๊ณ  ์–ผ๋งˆ๋‚˜ ์˜ค๋žซ ๋™์•ˆ ์‹คํ–‰์‹œํ‚ฌ ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•ด์„œ ์ „๋žต์ ์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์Šคํฌ๋กค๊ณผ ๊ฐ™์€(?) ์• ๋‹ˆ๋ฉ”์ด์…˜์ด ์กด์žฌํ•œ๋‹ค๋ฉด, ์ด์ƒ์ ์œผ๋กœ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์‹คํ–‰ ์‹œ๊ฐ„์ด 3~4ms ์ด๋‚ด์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ด ์‹œ๊ฐ„๋ณด๋‹ค ๊ธธ์–ด์ง€๊ฒŒ ๋œ๋‹ค๋ฉด, frame loss๋กœ ์ด์–ด์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์œ ํœด ์‹œ๊ฐ„์— ์žˆ๋‹ค๋ฉด, ์กฐ๊ธˆ ๋” ์—ฌ์œ ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.(์Šคํฌ๋กค ์• ๋‹ˆ๋ฉ”์ด์…˜์— ๋Œ€ํ•œ ๋‚ด์šฉ๊ฐ™์€๋ฐ ํ•ด์„์ด ์•ˆ๋˜๋„ค์š”)

๋ฐ์ดํ„ฐ ์กฐ์ž‘ ํ˜น์€ ํƒ์ƒ‰, ์ •๋ ฌ๊ณผ ๊ฐ™์ด DOM์— ์ ‘๊ทผํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด Web Worker์—๊ฒŒ ๊ณ„์‚ฐ ์ž‘์—…์„ ๋งก๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

var dataSortWorker = new Worker("sort-worker.js");
dataSortWorker.postMesssage(dataToSort);

// The main thread is now free to continue working on other things...

dataSortWorker.addEventListener('message', function(evt) {
    var sortedData = evt.data;
    // Update data on screen...
});

Web Worker๋Š” DOM ์ ‘๊ทผ ๊ถŒํ•œ์ด ์—†์œผ๋ฏ€๋กœ, DOM์— ์ ‘๊ทผํ•ด์•ผํ•˜๋Š” ์ž‘์—…์„ ๋งก๊ธฐ๋Š” ๊ฒƒ์€ ์œ ํšจํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. (๐Ÿ˜ฅํ•ด์„์ด ์–ด๋ ต๋„ค์š”) Where your work must be on the main thread, consider a batching approach, where you segment the larger task into micro-tasks, each taking no longer than a few milliseconds, and run inside of requestAnimationFrame handlers across each frame.

์ด ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ์ง„ํ–‰๋ฅ ์„ ์œ ์ €์—๊ฒŒ ๋ณด์—ฌ์คŒ์œผ๋กœ์จ ์ž‘์—…์ด ์ง„ํ–‰๋˜๊ณ  ์žˆ์Œ์„ ์•Œ๋ ค์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์–ด์ฐŒ๋๊ฑด ์ด ์ ‘๊ทผ ๋ฐฉ๋ฒ•์€, ์•ฑ์˜ main thread๋ฅผ ์ข€ ๋” freeํ•˜๊ฒŒ ๋งŒ๋“ค๊ณ , ์‘๋‹ต์„ฑ์„ ๋†’์ด๋Š”๋ฐ ๋„์›€์„ ์ค๋‹ˆ๋‹ค.

๐Ÿค” ์•„๋งˆ๋„ renderer process ๋‚ด๋ถ€์˜ ์—ฌ๋Ÿฌ ์Šค๋ ˆ๋“œ ์ค‘, ์ด๋ฒคํŠธ๋‚˜ ๋ Œ๋”๋ง์„ ๋‹ด๋‹นํ•˜๋Š” main thread๊ฐ€ ๋„ˆ๋ฌด ๋ฌด๊ฑฐ์›Œ์ง€๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด, Web Worker๋ฅผ ์ด์šฉํ•ด์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ณต์žกํ•œ ์—ฐ์‚ฐ์„ ๋‹ค๋ฅธ thread์—์„œ ๋ณ‘๋ ฌ๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค. ์ •๋„๋กœ ์ดํ•ดํ•˜๋ฉด ๋˜์ง€ ์•Š์„๊นŒ ์‹ถ์Šต๋‹ˆ๋‹ค. ๊ด€๋ จํ•œ ์ถ”๊ฐ€ ๋‚ด์šฉ์€ ์•„๋ž˜ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ๋  ๋“ฏ ํ•ฉ๋‹ˆ๋‹ค.

Web Worker
๋ธŒ๋ผ์šฐ์ € ํ”„๋กœ์„ธ์Šค
Minimize main thread work
Main Thread

3. ํ•˜๋‚˜์˜ ํ”„๋ ˆ์ž„์„ ๋งŒ๋“œ๋Š”๋ฐ ์š”๊ตฌ๋˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋น„์šฉ์„ ํŒŒ์•…ํ•˜์„ธ์š”

ํ”„๋ ˆ์ž„์›Œํฌ๋‚˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, ํ˜น์€ ๋‹น์‹ ์˜ ์ฝ”๋“œ๋ฅผ ํ‰๊ฐ€ํ• ๋•Œ, ํ”„๋ ˆ์ž„ ๋‹จ์œ„๋กœ Javascript๋ฅผ ์‹คํ–‰ํ•˜๋Š”๋ฐ ๋“œ๋Š” ๋น„์šฉ์„ ํ‰๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ํŠนํžˆ๋‚˜ ์„ฑ๋Šฅ์— ์น˜๋ช…์ ์ผ ์ˆ˜ ์žˆ๋Š” transition์ด๋‚˜ scrolling ์• ๋‹ˆ๋ฉ”์ด์…˜์—์„œ ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

Chrome DevTools๋Š” ์ด๋Ÿฌํ•œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋น„์šฉ์„ ์ธก์ •ํ•˜๋Š”๋ฐ ๋„์›€์„ ์ค๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ Main Section์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ˜ธ์ถœ์— ๋Œ€ํ•œ Flame Chart๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์‰ฝ๊ฒŒ ์–ด๋–ค ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋๊ณ , ์–ผ๋งˆ๋‚˜ ์˜ค๋žซ๋™์•ˆ ์‹คํ–‰๋˜๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ์ •๋ณด๋ฅผ ํ† ๋Œ€๋กœ ์•ฑ์—์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์„ฑ๋Šฅ์— ์–ผ๋งˆ๋‚˜ ์˜ํ–ฅ์„ ๋ผ์น˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์‹คํ–‰์— ๋งŽ์€ ์‹œ๊ฐ„์ด ์†Œ์š”๋˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ฐพ์•„์„œ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•ž์„œ ๋งํ–ˆ๋“ฏ์ด, ์šฐ๋ฆฌ๋Š” ์‹คํ–‰ ์‹œ๊ฐ„์ด ๊ธด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ œ๊ฑฐํ•˜๊ฑฐ๋‚˜, ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด Web Worker๋กœ ์ด๋™์‹œ์ผœ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

4. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋„ˆ๋ฌด micro๋‹จ์œ„๊นŒ์ง€ ์ตœ์ ํ™”ํ•˜๋ ค๊ณ  ํ•˜์ง€๋งˆ์„ธ์š”

์–ด๋–ค ๊ฐ์ฒด์˜ offsetTop์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์ด, getBoundingClientRect()๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ ๋ณด๋‹ค 100๋ฐฐ ๊ฐ€๋Ÿ‰ ๋น ๋ฅด๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ์žˆ๋‚˜์š”? ์•Œ๊ณ ์žˆ์œผ๋ฉด ์ข‹์„์ง€๋„ ๋ชจ๋ฅด์ง€๋งŒ, ์ด๋Ÿฌํ•œ ํ•จ์ˆ˜๋“ค์€ ํ”„๋ ˆ์ž„๋‹น ํ˜ธ์ถœ๋˜๋Š” ํšŸ์ˆ˜๊ฐ€ ๋งค์šฐ ์ ๊ณ , ์ด๋Ÿฌํ•œ ๋น„์šฉ์„ ์•„๊ปด๋ดค์ž ๊ณ ์ž‘ ms๋‹จ์œ„๋ฐ–์— ์•„๋ผ์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. ๋„ˆ๋ฌด microํ•˜๊ฒŒ ์ตœ์ ํ™”ํ•˜๋ ค ํ•˜๋Š” ๊ฒƒ์€ ๋น„ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฒŒ์ž„๊ณผ ๊ฐ™์ด ์—ฐ์‚ฐ ๋น„์šฉ์ด ๋งŽ์ด ๋“œ๋Š” ์•ฑ์„ ์„ค๊ณ„ํ•œ๋‹ค๋ฉด, ํ”„๋ ˆ์ž„ ํ•˜๋‚˜๋‹น ๋งŽ์€ ์—ฐ์‚ฐ์ด ๋“ค์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฌํ•œ ์กฐ์–ธ์ด ์˜ˆ์™ธ์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

rAF์— ๋Œ€ํ•œ ์ถ”๊ฐ€์ ์ธ ๋‚ด์šฉ

CPU ์ž์›์„ ๊ณ ๋ คํ•ฉ๋‹ˆ๋‹ค(CPU Optimization). ๋‹ค๋ฅธ ํƒญ์— ์žˆ๋Š” ๊ฒฝ์šฐ, rAF๋‚ด์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ํ˜ธ์ถœ์€ ๋ฉˆ์ถ”๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

setTimeout/setInterval์˜ ํŠน์ง•

  • ๋‘˜ ๋‹ค CPU ์ž์›์„ ๊ณ ๋ คํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํƒญ์— ์žˆ์–ด๋„, ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ๊ณ„์†ํ•ด์„œ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
  • setInterval์˜ ๊ฒฝ์šฐ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์‹คํ–‰ ์‹œ๊ฐ„์ด ๋”œ๋ ˆ์ด๋ณด๋‹ค ๊ธธ์–ด์ง€๊ฒŒ ๋˜๋ฉด ๋„ˆ๋ฌด ๋งŽ์€ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ํƒœ์Šคํฌ ํ์—์„œ ๋Œ€๊ธฐํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

What requestAnimationFrame is ?
Why requestAnimationFrame is better than setTimeout and setInterval in HTML5
Window.requestAnimationFrame

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

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด