𝗪𝗵𝗮𝘁 𝗔𝗿𝗲 𝗧𝘆𝗽𝗲𝗱 𝗔𝗿𝗿𝗮𝘆𝘀 𝗜𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁?
Regular JavaScript arrays hold any data type. This makes them slow for large data sets.
Typed arrays store only numbers of a fixed size. You pick the size and type when you create them.
Why use them?
- Speed: Computers process fixed data faster. This helps games and graphics.
- Memory: They store raw binary data. This uses less space.
- Access: Use these for images, audio, and files.
Common types:
- Uint8Array: Whole numbers 0 to 255. Used for images.
- Int8Array: Numbers -128 to 127.
- Uint16Array: Whole numbers 0 to 65,535. Used for audio.
- Float32Array: Decimal numbers. Used for physics.
Example:
Regular array: [1, "hello", true] Typed array: new Uint8Array([255, 128, 0])
Typed arrays have a fixed size. The size does not change. They store only one number type.
Node.js Buffer uses Uint8Array.
Tip: Typed arrays are views. They look into a memory chunk called an ArrayBuffer.
Source: https://dev.to/ho3na3/what-are-typed-arrays-in-javascript-342p