0
0
Node.jsframework~10 mins

Memory management best practices in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new buffer of 10 bytes in Node.js.

Node.js
const buffer = Buffer.[1](10);
Drag options to blanks, or click blank then click option'
Amake
Bcreate
Cnew
Dalloc
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'new Buffer()' which is deprecated and unsafe.
Trying to use 'Buffer.create()' which does not exist.
2fill in blank
medium

Complete the code to free up memory by clearing a large object reference.

Node.js
let largeObject = { data: 'some large data' };
// When done, set to [1] to help garbage collection
Drag options to blanks, or click blank then click option'
Anull
B0
Cundefined
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Setting the variable to 0 or false does not remove the object reference.
Setting to undefined is possible but null is clearer for intent.
3fill in blank
hard

Fix the error in the code to avoid memory leaks by removing event listeners properly.

Node.js
const EventEmitter = require('events');
const emitter = new EventEmitter();

function onData() {
  console.log('Data received');
}
emitter.on('data', onData);

// To prevent leaks, remove listener with emitter.[1]('data', onData);
Drag options to blanks, or click blank then click option'
AremoveListener
BdeleteListener
Coff
DremoveEvent
Attempts:
3 left
💡 Hint
Common Mistakes
Using removeEvent which does not exist.
Using deleteListener which is not a method.
4fill in blank
hard

Fill both blanks to create a WeakMap and add an object key with a value.

Node.js
const [1] = new [2]();
const objKey = {};
weakMap.set(objKey, 'value');
Drag options to blanks, or click blank then click option'
AweakMap
BWeakMap
CMap
Dweakmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using Map instead of WeakMap which does not allow garbage collection of keys.
Using lowercase weakmap which is not a valid constructor.
5fill in blank
hard

Fill all three blanks to create a memory-efficient cache using a WeakMap and add an entry.

Node.js
const [1] = new [2]();
const user = { id: 1 };
[3].set(user, { name: 'Alice' });
Drag options to blanks, or click blank then click option'
Acache
BWeakMap
DMap
Attempts:
3 left
💡 Hint
Common Mistakes
Using Map instead of WeakMap which keeps strong references.
Using inconsistent variable names.