Hari 24: TypedArray & Binary Data
50 min
Last updated 09 Apr 2026
TypedArray — Performa Tinggi
// Berbeda dengan Array biasa, TypedArray menyimpan data biner efisien
const int8 = new Int8Array([1, -128, 127]);
const uint8 = new Uint8Array([0, 128, 255]);
const float64 = new Float64Array([3.14, 2.72]);
// Operasi
const data = new Int32Array(5);
for (let i = 0; i < data.length; i++) data[i] = i * 10;
// [0, 10, 20, 30, 40]
// Konversi
Array.from(data); // [0, 10, 20, 30, 40]
[...data]; // sama
ArrayBuffer & DataView
// Buffer mentah
const buffer = new ArrayBuffer(8); // 8 byte
const view = new DataView(buffer);
// Tulis data
view.setInt32(0, 42, true); // offset 0, little-endian
view.setFloat32(4, 3.14, true); // offset 4
// Baca data
console.log(view.getInt32(0, true)); // 42
console.log(view.getFloat32(4, true)); // 3.14
💡
Notice: Dot product [1,2,3]·[4,5,6] = 1*4 + 2*5 + 3*6 = 4+10+18 = 32. Magnitude = √(1+4+9) = √14 ≈ 3.7417.
Assignment
Gunakan Float32Array untuk komputasi vektor. Buat dua vektor [1,2,3] dan [4,5,6], hitung dot product (penjumlahan perkalian elemen), dan magnitude vektor pertama.
Expected output:
Dot product: 32
Magnitude v1: 3.7417
JS
script.js
Solution
Output