ADVANCED TOPIC Flashcards

1
Q

How does JavaScript garbage collection work?

A

โ€œ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.โ€

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are weak references (WeakMap, WeakSet)?

A

โ€œ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.โ€

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain Generators and Iterators in JavaScript.

A

โ€œ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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you prevent memory leaks in JavaScript?

A

โ€œ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.โ€

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are ES6 Modules, and how are they different from CommonJS?

A

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.โ€

How well did you know this?
1
Not at all
2
3
4
5
Perfectly