ํฐ์คํ ๋ฆฌ ๋ทฐ
[Udemy] JavaScript: The Advanced Concepts - ์น์ 2 : JavaScript Foundation
Carrot๐ฅ 2020. 8. 6. 05:38** โ priv | ๊ฐ์๋
ธํธ
์นดํ
๊ณ ๋ฆฌ์ ์๋ ๋ด์ฉ์ ๋ฐํ์ด ๋ชฉ์ ์ด ์๋๊ธฐ ๋๋ฌธ์ ์ ํ ์ ์ ๋์ด์์ง ์์ต๋๋น.**
11. Inside the V8 Engine
V8 ์์ง์ C++๋ก ์์ฑ๋จ
๊ตฌ๊ธ ํฌ๋กฌ, Node JS ์์ ์ฌ์ฉ
V8์์ง ์๋์๋ฆฌ?
JS ์์ค์ฝ๋ โ Parser
๊ฐ ์์ค์ฝ๋๋ฅผ ๋ถ์ํด์
AST
_(Abstract Syntax Tree - ์ถ์๊ตฌ๋ฌธํธ๋ฆฌ : ์ปดํ์ผ๋ฌ์์ ๋ง์ด ์ฌ์ฉํ๋ ์๋ฃ๊ตฌ์กฐ)_๋ก ๋ณํ -
์ด๋ scope๋ ํ์ฑ๋๋ค๊ณ ํจ
AST๋ฅผ ๊ฐ์ ธ๊ฐ๋ค๊ฐ ignition
( V8 interpreter ) ํํ
๋๊ธฐ๊ณ
์๊ฐ JS์ฝ๋๋ฅผ ๋ฐ์ดํธ์ฝ๋ ( Bytecode
) ๋ก ๋ณํ
Profiler ๋ ์ด ์ค ์์ ์์ฃผ ์ฐ์ด๋ ์ฝ๋๋ฅผ ์ฐพ์(ํจ์,๋ณ์์ ํธ์ถ๋น๋๋ฅผ ๋ชจ์์)
๊ทธ๊ฑธ ๋ค๊ณ TurboFan
(์ต์ ํ ๋ด๋น ์ปดํ์ผ๋ฌ) ์ ๊ฐ์ ธ๊ฐ๋ฉด ์ต์ ํ(optimize)ํจ
๋ค์ํ ์ต์ ํ๊ธฐ๋ฒ > ๊ถ๊ธํ๋น!!
์ด๋ฐ ์๋ฆฌ๋ฅผ ๋ชฐ๋ผ๋ JS๋ฅผ ์ฌ์ฉํ ์ ์๋ค
๊ทธ์น๋ง
how the engine works underneath the hood ๋ฅผ ์๋ ๊ฒ์ ์ฐ๋ฆฌ๊ฐ more optimized code ์ข ๋ ์ต์ ํ ๋ ์ฝ๋๋ฅผ ์งค ์ ์๊ฒ ํด์ค๋ค!
V8 ์์ง ๊ตฌ๋์๋ฆฌ๋ฅผ ๊ณต๋ถํ๋๋ฐ ๋์์ด ๋ง์ด ๋ ํฌ์คํ
https://evan-moon.github.io/2019/06/28/v8-analysis/
ใด๋๋ชจ ์ข์ ํฌ์คํ
์ธ๋ฐ ๊ฐ์ฌํ๋ค๊ณ ๋๊ธ์ ๋ฌ ๊ณณ์ด ์๋คใ
12. Comparing Other Languages
C++
๋ก ์ฝ๋๋ฅผ ์ง์.exe
ํ์ผ์ ๋ง๋ค ์ ์๋ค
C++ code -> an executable machine code ๋ก ์ปดํ์ผ๋
๊ณ ๊ฒ์ ๋๋ธํด๋ฆญํ๋ฉด ์ฝ๋๊ฐ ์คํ์ด ๋๋ค
์๋ ์ด๋ฏธ have been compiled ๋์ด์์ด์ ๋ฐ๋ก ์คํ๋ ๊ฒ์ด๋ค.
JAVA
๋ JVM (Java Virtual Machine)๋ฅผ ์ด์ฉํจ
JVM์ ์ฝ๋๋ฅผ compile ํ๊ฑฐ๋ interpret ํด์ bytecode๋ก ๋ง๋ ๋ค(?)bytecode
is not a native machine code. ์ปดํจํฐ๊ฐ ์ดํดํ์ง ๋ชปํจ.
๊ทธ๋ ๊ฒ ๋๋ฌธ์ virtual machine ์ด๋ JavaScript engine ๊ณผ ๊ฐ์ ์ํํธ์จ์ด๊ฐ ์์ด์ผ ์คํ ๊ฐ๋ฅํจ
13. Writing Optimized Code
JS ์์ง์ ๋๊ธฐ์ํด์ (๋ ๋น ๋ฅด๊ฒ ์๋ํ๊ธฐ ์ํด์?)
์๊ฒ๋ค์ ์ ๋ง ์ฃผ์ํ์
eval()
, arguments
, for.. in
, with
, delete
arguments
: ์๊ฑฐ ๋ง์ด ์ฐ๊ฒ ๋๊ธดํ๋๋ฐ, we can use parameter destructuring
to avoid using arguments for and loop.
ใด MDN ์ ์๋ฌ์ผ ์จ์๋๋ฐ ์ด์๊ธด๊ฐ?
Note: ES6 ํธํ ์ฝ๋๋ฅผ ์์ฑ ์ค์ด๋ผ๋ฉด ๋๋๋ก ๋๋จธ์ง ๋งค๊ฐ๋ณ์ ๊ตฌ๋ฌธ์ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
ใด ๊ด๋ จ ์ฝ์ ๊ฑฐ๋ฆฌ : Avoid modifying or passing arguments
into other functions โ it kills optimization - https://www.jstips.co/en/javascript/avoid-modifying-or-passing-arguments-into-other-functions-it-kills-optimization/
ใด Passing arguments
to any other function is known as leaking arguments
. https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments
ใด ๋์ถฉ arguments ์๋ฅผ ์ด๋ค ํจ์(array.slice ๋ฑ๋ฑ)์ ํต๊ณผ์ํค๋ฉด V8 ์์ง์ optimization ๊ณผ์ ์ skipํ๋ค๋ ์๊ธฐ์ธ๋ฏ!
ใด
What is safe arguments
usage?
Only use
arguments.length
arguments[i]
wherei
is always a valid integer index into the arguments, and can not be out of bound- Never use
arguments
directly without.length
or[i]
- STRICTLY
fn.apply(y, arguments)
is ok, nothing else is, e.g..slice
.Function#apply
is special. - Be aware that adding properties to functions (e.g. fn.$inject =...) and bound functions (i.e. the result of
Function#bind
) generate hidden classes and, therefore, are not safe when using#apply
.
ใด ์ ๊ธฐํ๊ตฌ๋ง๐ค๐ค
for ... in
object๋ฅผ loopํ ์ผ์ด ์์ผ๋ฉดObject.keys()
๋ฅผ ์ด์ฉ
V8 ์์ง์ ์ค์ํ optimizing ์ปดํ์ผ ๊ธฐ๋ฒ
Inline Caching
, Hidden Classes
์ธ๋ผ์ธ ์บ์ฑ์
์ด๋ค ํจ์๊ฐ ๋๊ฐ์ ์ธ์๋ก ๊ณ์๊ณ์ ํธ์ถ๋๋ฉด
๊ณ ํธ์ถ๋ ๋ผ์ธ ์์ฒด๋ฅผ return๊ฐ์ผ๋ก ๋์ฒดํด์ ํด์ > ์ธ๋๋์ ๊ณผ์ ํ๋๊ฑฐ ์ค์์ memoize ์ด๋ ๋ญ๊ฐ ๋น์ทํ ๋๋์ธ๋ฐ..๐ค
์๊ฑธ ์ดํดํ๋ ค๋ฉด ํ๋ ํด๋์ค๋ฅผ ์์์ผ๋จ
ใด ์ฝ์ ๊ฑฐ๋ฆฌ : Javascript Hidden Classes and Inline Caching in V8
https://richardartoul.github.io/jekyll/update/2015/04/26/hidden-classes.html
โ Optimization takeaways
- ์จ๊ฒจ์ง ํด๋์ค์ ๊ทธ ์ดํ์ ์ต์ ํ๋ ์ฝ๋๋ฅผ ๊ณต์ ํ ์ ์๋๋ก ํญ์ ๊ฐ์ฒด ์์ฑ์ ๋์ผํ ์์๋ก ์ธ์คํด์คํํฉ๋๋ค.
- ์ธ์คํด์คํ ํ ๊ฐ์ฒด์ ์์ฑ์ ์ถ๊ฐํ๋ฉด ์จ๊ฒจ์ง ํด๋์ค ๋ณ๊ฒฝ์ด ๊ฐ์ ๋ก ์ ์ฉ๋๊ณ ์ด์ ์จ๊ฒจ์ง ํด๋์ค์ ์ต์ ํ๋ ๋ชจ๋ ๋ฉ์๋๊ฐ ๋๋ ค์ง๋๋ค. ๋์ ํด๋น ์์ฑ์์ ์๋ ๊ฐ์ฒด์ ์์ฑ์ ๋ชจ๋ ํ ๋นํฉ๋๋ค.
- ์ธ๋ผ์ธ ์บ์ฑ์ผ๋ก ์ธํด ๋์ผํ ๋ฐฉ๋ฒ์ ๋ฐ๋ณต์ ์ผ๋ก ์คํํ๋ ์ฝ๋๋ ์ฌ๋ฌ๊ฐ์ง ๋ฐฉ๋ฒ์ ํ๋ฒ๋ง ์คํํ๋ ์ฝ๋๋ณด๋ค ๋น ๋ฅด๊ฒ ์คํ๋ฉ๋๋ค.
function Animal(x, y){
this.x = x;
this.y = y;
}
const obj1 = new Animal(1, 2);
const obj2 = new Animal(3, 4);
obj1.a = 30;
obj1.b = 100;
obj2.b = 30;
obj2.a = 100;
// ์ด๋ ๊ฒ ๊ฐ์ฒด์ ์์ฑ์ ๋ค๋ฅธ ์์๋ก ํ ๋น hidden class๋ฅผ ๊ณต์ ํ์ง ์๊ฒ ๋์ด ์๋๊ฐ ์ ํ๋จ
// delete ์ฐ์ฐ์ issue๋ ๋ง์ฐฌ๊ฐ์ง ์ด์
์ฌ๊ธฐ์ ํต์ฌ์
์์ธก๊ฐ๋ฅํ predictable ์ฝ๋๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค๋ ๊ฒ.
์์ธก๊ฐ๋ฅํ ์๋ก ์ปดํจํฐ ์
์ฅ์์๋ ์ต์ ํ ํ๊ธฐ ์ข๋ค.
์ปดํ์ผ๋ฌ๋ฅผ ํผ๋์ค๋ฝ๊ฒ ํ์ง ์๋ ์์ธก๊ฐ๋ฅํ ์ฝ๋๊ฐ fast efficient code
ใด ์ฝ์ ๊ฑฐ๋ฆฌ :
์๋ฐ์คํฌ๋ฆฝํธ๋ ์ด๋ป๊ฒ ์๋ํ๋๊ฐ: V8 ์์ง์ ๋ด๋ถ + ์ต์ ํ๋ ์ฝ๋๋ฅผ ์์ฑ์ ์ํ ๋ค์ฏ ๊ฐ์ง ํ
14. WebAssembly
Why not just use machine code from the beginnig?
๋ธ๋ผ์ฐ์ ๋ค์ด the standard binary executable format ์ ๋์ํ์
์๊ฒ์ ๊ณง Game Changer ๊ฐ ๋ ๊ฑฐ์. ์์ ์ฃผ์ํ์
WebAssembly๋ ์ต์ ์น ๋ธ๋ผ์ฐ์ ์์ ์คํํ ์ ์๋ ์๋ก์ด ์ ํ์ ์ฝ๋์ ๋๋ค. ๋ค์ดํฐ๋ธ์ ๊ฐ๊น์ด ์ฑ๋ฅ์ผ๋ก ๋์ํ๋ฉฐ ์ปดํฉํธํ ๋ฐ์ด๋๋ฆฌ ํฌ๋งท์ ์ ๊ณตํ๋ ์ ์์ค ์ด์ ๋ธ๋ฆฌ ์ธ์ด๋ก, C/C++, Rust ๋ฑ๊ณผ ๊ฐ์ ์ธ์ด์ ์ปดํ์ผ ํ๊ฒ์ผ๋ก์จ ๊ทธ๋ฐ ์ธ์ด๋ก ์์ฑ๋ ํ๋ก๊ทธ๋จ์ ์น์์ ์ฌ์ฉํ ์ ์๊ฒ ํด์ค๋๋ค. ๋ํ JavaScript์ ํจ๊ป ์คํ๋๋ฉฐ ์๋ก๋ฅผ ๋ณด์ํ ์ ์๋๋ก ์ค๊ณ๋์์ต๋๋ค.
++
์น ์ด์
๋ธ๋ฆฌ๋ ์๋ฐ์คํฌ๋ฆฝํธ์ ๋ฌด๋ค์ผ๊น?
https://youtu.be/KjgDxBLv0bM
WebAssembly = WASM
Compilation Target
๋ธ๋ผ์ฐ์ ๊ฐ ์ดํดํ ์ ์์
์น ์ด์
๋ธ๋ฆฌํ์ผ์ ๊ทธ๋ฅ ๋ฐ์ด๋๋ฆฌ ํ์ผ์
C, C++, Rust, Go ๊ฐ ์น์ด์
๋ธ๋ฆฌ๋ก ์ปดํ์ผ๋จ
JS๊ฐ ๋ชปํ๋๊ฑธ ํ๊ฒํด์ค
์ํํธ์จ์ด๋ฅผ ์ค์นํ์ง ์์๋ ๋ธ๋ผ์ฐ์ ๊ฐ C, C++ ๊ฐ์ ์ธ์ด๋ฅผ ์ดํดํ ์ ์์ผ๋๊น cooool
์ด์ ์ ๋ํฐ๊ฒ์์ ์ปดํ์ผํ๋ฉด ์น์ฌ์ดํธ๊ฐ ํด๋น ๋ฐ์ด๋๋ฆฌ ํ์ผ์ ์ดํดํ ์ ์์.
์น ์ด์
๋ธ๋ฆฌ์ JS ๋ ์๋ก ๋์ฒด์ ๊ฐ ์๋๋ผ
์๋ก ๋ชปํ๋ ๋ถ๋ถ์ ๋ณด์ํด์ฃผ๋๊ฒ
์ํผ ์ฟจ!
comprehension 1.์ดํด๋ ฅ
feasible 1.์คํ ๊ฐ๋ฅํ
15. Call Stack and Memory Heap
๋ฌด์ง๋ง์งํ๊ฒ ์ค์ํ ์ฝ์คํ๊ณผ ๋ฉ๋ชจ๋ฆฌํ
heap
: ๋ฉ๋ชจ๋ฆฌ๋ฅผ allocate, use, release memory
call stack
: we need place to keep trak of where we are in the code, so with that we can run the code in order
stack frame
allow us to know where we are in the code
16. Stack Overflow
์ฝ์คํ์ ์ค์ฒฉ๋ ํจ์๊ฐ ๊ณ์๊ณ์ ์์ธ๋ค๋ฉด
function inception(){
inception();
}
Recursion
is one of the most common ways to create a stack overflow
Uncaught RangeError: Maximum call stack size exceeded
๋ฐ์!
17. Garbege Collection
๋ฉ๋ชจ๋ฆฌ๋ ์คํ์ฒ๋ผ limit๊ฐ ์๊ธฐ๋๋ฌธ์
๋์ด์ ์ฌ์ฉํ์ง ์๋ ๋ฉ๋ชจ๋ฆฌ๋ GC์ ์ํด free up ๋จ
C์ ๊ฐ์ low level ์ธ์ด์์๋ ์ง์ ์ปจํธ๋กคํ๋ค
Mark and Sweep Algorithms
์ ์ํด ์๋
Reference-counting
์์๋ ์๋ก๊ฐ ์๋ก๋ฅผ ์ฐธ์กฐํ๋ ๊ฐ์ฒด๋ GC์ ์์ง๋์์ด ์๋๋ค (์ฐธ์กฐ์ธ๊ธฐ ์๊ณ ๋ฆฌ์ฆ)
๋ฐ๋ผ์ 2012๋
๊ธฐ์ค์ผ๋ก ๋ชจ๋ ์ต์ ๋ธ๋ผ์ฐ์ ๋ค์ ๊ฐ๋น์ง ์ฝ๋ ์
์์ Mark and Sweep
์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํ๋ค.
JS์์๋ ์ ์ญ๋ณ์๋ฅผ roots ๋ก ๋๊ณ , ๊ฑฐ๊ธฐ์๋ถํฐ ์์ํด์ ๋ฟ์ ์ ์๋ ์ค๋ธ์ ํธ๋ฅผ ๊ฐ๋น์ง๋ก ์์งํ๋ค.
์ฐธ์กฐ : https://developer.mozilla.org/ko/docs/Web/JavaScript/Memory_Management
18. Memory Leaks
๋ฉ๋ชจ๋ฆฌ ๋์์ ๊ฐ์ฅ ํฐ ์์ธ ์ธ๊ฐ์ง
-
Global Variables
-
Event listener
-
setInterval()
hypothetically ๊ฐ์ ํ์ฌ
19. Single Threaded
it has only one call stack
์ฐ๋ฆฌ๋ ํจ์๋ฅผ ๋ณ๋ ฌ์ ์ผ๋ก ์คํํ ์ ์๊ณ
์๋ก์ด ํจ์๋ฅผ ์คํ(call stack์ push)ํ ๋๋ง๋ค
call stack์ด ์ ์ ์ปค์ง๊ฒ
mimic ๋ชจ๋ฐฉํ๋ค, ํ๋ด๋ด๋ค
hence 1.์ด๋ฐ ์ด์ ๋ก
meanwhile (๋ค๋ฅธ ์ผ์ด ์ผ์ด๋๊ณ ์๋) ๊ทธ ๋์์, ํํธ
21. Javascript Runtime
Issue With Single Thread ์ ํด๊ฒฐํ๋ ค๋ฉด?
web API๋ ๋ธ๋ผ์ฐ์ ๋ก๋ถํฐ ์ ๊ณต๋จ
window ๊ฐ์ฒด์์ ์ด๋ธ ex) setTimeout, setInterval, fetchโฆ
์๋ค๋ ๋ฐฑ๊ทธ๋ผ์ด๋์์ ๋น๋๊ธฐ๋ก ์คํ๋จ
underneath the hood, ๋ธ๋ผ์ฐ์ ๋ค์ C++ ๊ณผ ๊ฐ์ ๋ก์ฐ๋ ๋ฒจ์ธ์ด๋ฅผ ์ด์ฉํด์ ์ด๋ฐ ์์ ์ ์ํํ๋ค
์๋ค๋ค์ JS์ ์์ญ์ด ์๋ Web API์ ์์ญ์์ ์๋๊ฑฐ์
callback queue
/ event loop
์๊ฑด ์์์ฅ
event loop
: hey! callstak! are you empty????? are you empty????? are you empty?????
22. Node.js
JS file === Music Notes
JS Engine === Musician or Composer : ์ ๋ณด๋ฅผ ์ฝ๊ณ ์ดํดํ ์ ์์
JS Runtime === the whole package where we have the musician but we also give that musician besides the musical notes.
์์
์ ์ฐ์ฃผํ๊ธฐ ์ํ ๋ชจ๋ ๋๊ตฌ๋ค
๊ธ๊น Node.js ๋ JS Runtime ์ ๋๋ค!!!!!!!
Node.js ์ ์ ์!
Node.js is a JavaScript runtime built on Chrome V8 JavaScript engine.
web API, Callback Queue, Event Loop ๊ฐ์๊ฑด ์๋ ๋ธ๋ผ์ฐ์ ์์๋ง ์ธ ์ ์์๋๋ฐ
๋ธ๋ผ์ฐ์ ๋ฐ์์๋ JS๋ฅผ ๋๋ฆด์ ์๊ฒ ๋ง๋ C++ํ๋ก๊ทธ๋จ
๋ธ๋ผ์ฐ์ ๊ฐ ์ฐ๋ฆฌ ์ปดํจํฐ์ ์ ๊ทผํ ์ ์๋๊ฑด ํ์ ์ ์ด๋ค
file system ๊ฐ์๊ฑฐ ์ ๊ทผ ๋ชจํ
sandbox enviroment
๊ทธ์น๋ง node.js ๋ ๋ฐฑ๊ทธ๋ผ์ด๋์์ ๋ ๋ง์๊ฑธ ํ ์์์!
node ํ๊ฒฝ์์๋ window
๊ฐ ์๋๋ผ global
์์ API๋ฅผ ์ฐพ์ ์ ์ด๋ธ
capability (capabilities) 1.๋ฅ๋ ฅ, ์ญ๋
์ถ์ฒ : Udemy - JavaScript: The Advanced Concept
https://www.udemy.com/course/advanced-javascript-concepts
'๊ณต๋ถ > priv | ๊ฐ์๋ ธํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
- js
- review
- HTML
- book
- stackoverflow
- GIT
- KEYBOARD
- ๋ฐ๋๋ผ์ฝ๋ฉ ํ๊ธฐ
- DOM
- css
- ์์ฑ์ํจ์
- ์ฝ๋ฉ๋ถํธ์บ ํ
- string
- array
- VSC
- eslint
- eventlistener
- Stash
- ๋ถํธ์บ ํ
- ๋ฐ๋๋ผ์ฝ๋ฉ
- Total
- Today
- Yesterday