Static data is data where the engine knows the size at compile time. You might know the stack from the first part of this series on the call stack and event loop, where I focused on how it's used to keep track of the functions that the JS interpreter needs to call.Īll the values get stored in the stack since they all contain primitive values.Ī stack is a data structure that JavaScript uses to store static data. Heaps and stacks are two data structures that the engine uses for different purposes. JavaScript engines have two places where they can store data: The memory heap and stack. The next question that came to my mind was: Where is this going to be stored? We now know that for everything we define in JavaScript, the engine allocates memory and frees it up once we don't need it anymore. " Objects" in the context of memory management doesn't only include JS objects but also functions and function scopes. Once the allocated memory is released, it can be used for a new purpose. This step is handled as well by the JavaScript engine. Using memory is something we do explicitly in our code: Reading and writing to memory is nothing else than reading or writing from or to a variable. JavaScript takes care of this for us: It allocates the memory that we will need for the object we created. In JavaScript, when we create variables, functions, or anything you can think of, the JS engine allocates memory for this and releases it once it's not needed anymore.Īllocating memory is the process of reserving space in memory, while releasing memory frees up space, ready to be used for another purpose.Įvery time we assign a variable or create a function, the memory for that always goes through the same following stages:
0 Comments
Leave a Reply. |