ADVANCED TOPIC Flashcards
How does JavaScript garbage collection work?
โJavaScriptโs garbage collection automatically frees up memory by reclaiming objects no longer referenced. It uses a mark-and-sweep algorithm: the garbage collector marks objects reachable from the root (e.g., global object), then sweeps away unmarked objects.
For example:
let obj = { data: 'test' }; obj = null; // No references, obj is eligible for garbage collection
Circular references, like two objects referencing each other, are handled by mark-and-sweep if no external references exist. As an SDET, Iโd test memory usage in long-running apps to ensure objects are properly dereferenced and garbage-collected to avoid leaks.โ
What are weak references (WeakMap, WeakSet)?
โWeakMap and WeakSet are ES6 collections that hold weak references to objects, meaning they donโt prevent garbage collection if no other references exist. WeakMap stores key-value pairs where keys must be objects, and WeakSet stores unique objects.
For example:
let weakMap = new WeakMap(); let key = {}; weakMap.set(key, 'value'); key = null; // key can be garbage-collected, removing the entry
Theyโre useful for caching or metadata without blocking memory. As an SDET, Iโd test WeakMap/WeakSet to ensure entries are cleared when objects are dereferenced, preventing unintended memory retention.โ
Explain Generators and Iterators in JavaScript.
โIterators are objects with a next() method that returns { value, done } for iterating over sequences. Generators are special functions (marked with *) that yield values one at a time, pausing execution between yield calls, making them powerful for lazy evaluation.
For example:
javascript
Copy
function* numberGenerator() {
yield 1;
yield 2;
yield 3;
}
const gen = numberGenerator();
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
Generators simplify custom iteration. As an SDET, Iโd test generators to ensure correct value yielding and state management, especially in async iteration scenarios.โ
How do you prevent memory leaks in JavaScript?
โMemory leaks occur when unused objects remain in memory due to lingering references. To prevent them:
Clear references: Set unused variables to null (e.g., obj = null).
Remove event listeners: Use removeEventListener when components are destroyed.
Use WeakMap/WeakSet: For temporary data to allow garbage collection.
Avoid global variables: They persist for the appโs lifetime.
Monitor closures: Ensure they donโt retain unneeded references.
For example:
element.addEventListener('click', handler); element.removeEventListener('click', handler); // Prevents leak
As an SDET, Iโd use tools like Chrome DevTools to profile memory, test for leaks in long-running apps, and verify cleanup in component lifecycles.โ
What are ES6 Modules, and how are they different from CommonJS?
Response:
โES6 Modules are a standardized way to organize and share JavaScript code using import and export syntax, supporting static analysis and tree-shaking. CommonJS, used in Node.js, uses require() and module.exports and is synchronous.
For example:
// ES6 Module (file: math.js) export const add = (a, b) => a + b; // Usage: import { add } from './math.js'; // CommonJS (file: math.js) module.exports.add = (a, b) => a + b; // Usage: const { add } = require('./math.js');
ES6 Modules are asynchronous, support cyclic dependencies better, and are browser-native. As an SDET, Iโd test module imports to ensure correct dependency resolution and verify tree-shaking removes unused code.โ