KEMBAR78
Day8 Memory and Performance | PDF | Operating System Technology | Software Development
0% found this document useful (0 votes)
11 views3 pages

Day8 Memory and Performance

The document provides an overview of JavaScript memory management, including garbage collection and memory leaks. It outlines best practices for avoiding memory leaks, profiling memory usage, and optimizing performance. Additionally, it explains the roles of closures and the event loop in relation to memory and performance, along with tools for performance auditing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Day8 Memory and Performance

The document provides an overview of JavaScript memory management, including garbage collection and memory leaks. It outlines best practices for avoiding memory leaks, profiling memory usage, and optimizing performance. Additionally, it explains the roles of closures and the event loop in relation to memory and performance, along with tools for performance auditing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

JavaScript Interview Preparation in 10 Days

Day 8 – JavaScript Memory, Garbage Collection & Performance

Brought to you by builtbyhimanshu.xyz

Q1. How does JavaScript manage memory?

JavaScript automatically allocates memory when variables and objects are created and frees it when
they are no longer used — a process called Garbage Collection.

Memory lifecycle:

1. Allocate memory
2. Use the memory
3. Release the memory

Q2. What is Garbage Collection (GC) in JavaScript?

Garbage Collection is a mechanism to automatically detect and reclaim memory that is no longer
reachable by the program.

Modern JavaScript engines use algorithms like:

• Mark-and-Sweep: Marks reachable objects and sweeps the rest.

let obj = { name: "Himanshu" };


obj = null; // eligible for GC

Q3. What are memory leaks and how do they occur?

A memory leak is when allocated memory is not released even though it’s no longer needed.

Common causes:

• Global variables
• Forgotten timers or callbacks
• Closures holding onto unused variables
• Detached DOM elements

let cached = {};


function remember() {
cached.large = new Array(1000000).fill("data");
}

1
Q4. How can you avoid memory leaks?

• Avoid unnecessary global variables


• Always clean up timers or listeners
• Detach DOM references properly
• Use tools like Chrome DevTools to monitor memory usage

window.addEventListener("resize", handler);
// later
window.removeEventListener("resize", handler);

Q5. How can you profile memory usage in JavaScript?

Use Chrome DevTools → Performance → Memory tab:

• Record heap snapshots


• Identify detached DOM nodes
• Track allocations over time

Tools: Chrome, Firefox DevTools, Lighthouse

Q6. What are some best practices for writing performance-optimized JS?

• Minimize DOM access


• Debounce or throttle input events
• Use requestAnimationFrame for animations
• Avoid deep nested loops and large synchronous blocks
• Use async/await for non-blocking tasks

function debounce(fn, delay) {


let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), delay);
};
}

Q7. What is throttling vs debouncing?

• Throttling: Limits the execution to once every X milliseconds


• Debouncing: Delays execution until after X milliseconds of inactivity

Use in scroll, resize, search box input handling.

2
Q8. How do closures affect memory and performance?

Closures can retain references to outer scope variables even after the function has returned. This can
cause memory leaks if not managed carefully.

function outer() {
let count = 0;
return function inner() {
console.log(++count);
};
}
const counter = outer();

Avoid retaining large objects in closures unnecessarily.

Q9. What is the event loop’s role in performance?

Event loop enables non-blocking I/O by processing callbacks from queues. Blocking the main thread
with synchronous code can freeze UI and degrade performance.

Use web workers or offload heavy computations.

Q10. What are some tools for performance auditing?

• Chrome DevTools → Performance Panel


• Lighthouse (PageSpeed insights)
• WebPageTest
• console.time() / console.profile() for manual tracking

console.time("loop");
for (let i = 0; i < 10000; i++) {}
console.timeEnd("loop");

For more content like this, visit builtbyhimanshu.xyz

You might also like