Lädt...


🔧 Understanding Stack and Heap in JavaScript .


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

In JavaScript, stack and heap are two types of memory used for managing data, each with a distinct purpose:

  1. Stack
  2. Heap

*What are Stack and Heap *
Stack : The Stack is used for static memory allocation, primarily for storing primitive types and function calls. It's a simple, last-in, first-out (LIFO) structure, making it very fast to access.

Heap : The Heap is used for dynamic memory allocation, where objects and arrays (non-primitive types) are stored. Unlike the Stack, the Heap is more complex and slower to access, as it allows for flexible memory allocation.

Example of Stack Memory :

let myName = "Amardeep"; //primitive type stored in stack 
let nickname = myName; // A copy of the value is created in the Stack 
nickname = "Rishu"; // Now changing the copy does not affect the original value .
console.log(myName); // output => Amardeep (Original values remains unchanged since we are using stack)
console.log(nickname); // output => rishu (only the copied value will changed)

In this example :

  • myName is stored in the Stack as a primitive type.
  • When nickname is assigned the value of myName , a copy of that value is created in the Stack .
  • Changing nickname doesn't affect myName , since they are independent copies in the memory.

Example of Heap Memory
Now lets check how non-primitive data types(objects) are managed in the Heap .

let userOne = {         // The reference to this object is stored in the Stack.
    email: "[email protected]",
    upi: "user@ybl"
};                      // The actual object data is stored in the Heap.

let userTwo = userOne;  // userTwo references the same object in the Heap.

userTwo.email = "[email protected]"; // Modifying userTwo also affects userOne.

console.log(userOne.email); // Output: [email protected]
console.log(userTwo.email); // Output: [email protected]

In this example:

  • userOne holds a reference to an object stored in the Heap. -userTwo is assigned the same reference, meaning both userOne and userTwo point to the same object in the Heap. -Changing userTwo.email directly affects userOne.email, because both references point to the same location in memory.

Key Takeaways
*Stack Memory * is used for storing primitive types and function calls . Each time you assign a value , a new copy is created in the Stack.
*Heap Memory * is used for storing objects and arrays . Variables that reference the same object share the same memory location in memory , so changing one variable affects the others .

...

🔧 Day 6:Understanding Stack and Heap in JavaScript


📈 27.03 Punkte
🔧 Programmierung

🔧 Understanding Stack and Heap in JavaScript .


📈 27.03 Punkte
🔧 Programmierung

🔧 Understanding Stack and Heap in JavaScript .


📈 27.03 Punkte
🔧 Programmierung

🔧 Understanding Stack Data Structure: A Step-by-Step Guide to Implementing Stack in JavaScript


📈 24.36 Punkte
🔧 Programmierung

🔧 Understanding the Heap and the Stack in Computer Systems


📈 21.35 Punkte
🔧 Programmierung

🔧 Understanding Java’s Pass by Value with Heap and Stack Frames Visualization


📈 21.35 Punkte
🔧 Programmierung

🔧 Understanding Stack and Heap in Go: A Simple Guide


📈 21.35 Punkte
🔧 Programmierung

🔧 Understanding Stack and Heap in Go: A Simple Guide


📈 21.35 Punkte
🔧 Programmierung

🔧 Understanding the Java Memory Model: Heap and Stack Explained


📈 21.35 Punkte
🔧 Programmierung

🔧 How to fix “fatal reached heap limit allocation failed javascript heap out of memory”


📈 20.19 Punkte
🔧 Programmierung

🔧 Mastering JavaScript Memory: A Beginner’s Guide to Stack and Heap


📈 20.14 Punkte
🔧 Programmierung

🔧 Understanding the Event Loop, Callback Queue, and Call Stack in JavaScript


📈 19.77 Punkte
🔧 Programmierung

🔧 Understanding Javascript Call-Stack


📈 18.46 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Operators : A Comprehensive Guide - MERN STACK Series


📈 18.46 Punkte
🔧 Programmierung

🔧 Understanding the Call Stack in JavaScript.


📈 18.46 Punkte
🔧 Programmierung

🕵️ iOS exploitation walkthrough: Heap Overflows and the iOS Kernel Heap


📈 15.83 Punkte
🕵️ Reverse Engineering

🔧 What’s the relationship between “a” heap and “the” heap?


📈 15.83 Punkte
🔧 Programmierung

🔧 Understanding JavaScript's `==` and `===`: Equality and Identity


📈 15.18 Punkte
🔧 Programmierung

🔧 Understanding var and let in JavaScript: When and Why to Use Each for Cleaner Code


📈 15.18 Punkte
🔧 Programmierung

⚠️ [papers] - [Spanish] Windows Heap Overflow Exploitation - Exploiting a Custom Heap Under Windows 7


📈 14.52 Punkte
⚠️ PoC

🕵️ Google Android vor 2016-07-05 auf Nexus 5/Nexus 7 2013 Qualcomm lib/heap/heap.c Pufferüberlauf


📈 14.52 Punkte
🕵️ Sicherheitslücken

⚠️ [papers] - Tetris Heap Spraying: Spraying the Heap on a Budget


📈 14.52 Punkte
⚠️ PoC

⚠️ [papers] - [Spanish] Windows Heap Overflow Exploitation - Exploiting a Custom Heap Under Windows 7


📈 14.52 Punkte
⚠️ PoC

🕵️ Google Android vor 2016-07-05 auf Nexus 5/Nexus 7 2013 Qualcomm lib/heap/heap.c Pufferüberlauf


📈 14.52 Punkte
🕵️ Sicherheitslücken

⚠️ [papers] - Tetris Heap Spraying: Spraying the Heap on a Budget


📈 14.52 Punkte
⚠️ PoC

🕵️ Qualcomm Snapdragon Auto up to SDM660 Heap Guard Heap-based memory corruption


📈 14.52 Punkte
🕵️ Sicherheitslücken

🔧 When building a Heap, is the structure of Heap unique?


📈 14.52 Punkte
🔧 Programmierung

matomo