0
0
Node.jsframework~10 mins

SharedArrayBuffer for shared memory 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 SharedArrayBuffer of 1024 bytes.

Node.js
const sharedBuffer = new [1](1024);
Drag options to blanks, or click blank then click option'
AUint8Array
BSharedArrayBuffer
CArrayBuffer
DBuffer
Attempts:
3 left
💡 Hint
Common Mistakes
Using ArrayBuffer instead of SharedArrayBuffer
Using Buffer which is Node.js specific but not shared memory
2fill in blank
medium

Complete the code to create a typed array view over the shared buffer.

Node.js
const sharedArray = new [1](sharedBuffer);
Drag options to blanks, or click blank then click option'
AInt16Array
BArray
CFloat32Array
DUint8Array
Attempts:
3 left
💡 Hint
Common Mistakes
Using Array which is not a typed array
Using Float32Array which is for 4-byte floats, not bytes
3fill in blank
hard

Fix the error in the code to safely increment a shared counter using Atomics.

Node.js
Atomics.[1](sharedArray, 0, 1);
Drag options to blanks, or click blank then click option'
Aadd
Bincrement
Cstore
Dload
Attempts:
3 left
💡 Hint
Common Mistakes
Using Atomics.increment which does not exist
Using Atomics.store which replaces the value instead of adding
4fill in blank
hard

Fill both blanks to create a shared buffer and a typed array view of 32-bit integers.

Node.js
const buffer = new [1](128);
const intArray = new [2](buffer);
Drag options to blanks, or click blank then click option'
ASharedArrayBuffer
BArrayBuffer
CInt32Array
DUint8Array
Attempts:
3 left
💡 Hint
Common Mistakes
Using ArrayBuffer instead of SharedArrayBuffer
Using Uint8Array instead of Int32Array for 32-bit integers
5fill in blank
hard

Fill all three blanks to atomically compare and exchange a value in the shared array.

Node.js
const oldValue = Atomics.[1](sharedArray, [2], [3], 42);
Drag options to blanks, or click blank then click option'
AcompareExchange
B0
C41
Dexchange
Attempts:
3 left
💡 Hint
Common Mistakes
Using Atomics.exchange which does not compare before swapping
Using wrong index or old value parameters