JavaScript Typed Array Methods
Essential methods for working with typed arrays efficiently
π§ What are Typed Array Methods?
Typed Array Methods are built-in functions that allow you to manipulate and work with typed arrays efficiently. They provide powerful ways to transform, filter, and analyze binary data.
// Basic typed array method example
const numbers = new Int32Array([1, 2, 3, 4, 5]);
const doubled = numbers.map(x => x * 2);
console.log(doubled); // Int32Array [2, 4, 6, 8, 10]
Output:
Int32Array [2, 4, 6, 8, 10]
Essential Typed Array Methods
map()
Transform each element
const arr = new Int16Array([1, 2, 3]);
const squared = arr.map(x => x * x);
filter()
Select elements by condition
const arr = new Int8Array([1, 2, 3, 4]);
const evens = arr.filter(x => x % 2 === 0);
reduce()
Combine elements into single value
const arr = new Uint8Array([1, 2, 3, 4]);
const sum = arr.reduce((a, b) => a + b, 0);
find()
Find first matching element
const arr = new Int32Array([10, 20, 30]);
const found = arr.find(x => x > 15);
πΉ Array Transformation Methods
Transform typed arrays with powerful methods:
const temperatures = new Float32Array([20.5, 25.3, 18.7, 30.2]);
// Convert Celsius to Fahrenheit
const fahrenheit = temperatures.map(c => (c * 9/5) + 32);
console.log(fahrenheit);
// Float32Array [68.9, 77.54, 65.66, 86.36]
// Filter hot days (>25Β°C)
const hotDays = temperatures.filter(temp => temp > 25);
console.log(hotDays);
// Float32Array [25.3, 30.2]
Output:
Float32Array [68.9, 77.54, 65.66, 86.36]
Float32Array [25.3, 30.2]
πΉ Search and Find Methods
Locate elements in typed arrays:
const scores = new Uint16Array([85, 92, 78, 96, 88]);
// Find first score above 90
const highScore = scores.find(score => score > 90);
console.log(highScore); // 92
// Find index of specific score
const index = scores.findIndex(score => score === 96);
console.log(index); // 3
// Check if any score is perfect
const hasPerfect = scores.some(score => score === 100);
console.log(hasPerfect); // false
// Check if all scores pass (>= 70)
const allPass = scores.every(score => score >= 70);
console.log(allPass); // true
Output:
92
3
false
true
πΉ Aggregation Methods
Calculate values from typed arrays:
const prices = new Float64Array([19.99, 29.50, 15.75, 42.30]);
// Calculate total
const total = prices.reduce((sum, price) => sum + price, 0);
console.log(`Total: $${total.toFixed(2)}`); // Total: $107.54
// Calculate average
const average = total / prices.length;
console.log(`Average: $${average.toFixed(2)}`); // Average: $26.89
// Find maximum price
const maxPrice = prices.reduce((max, price) => Math.max(max, price), 0);
console.log(`Max: $${maxPrice}`); // Max: $42.3
Output:
Total: $107.54
Average: $26.89
Max: $42.3
πΉ Iteration Methods
Loop through typed arrays efficiently:
const data = new Int8Array([1, 2, 3, 4, 5]);
// forEach - execute function for each element
console.log("Using forEach:");
data.forEach((value, index) => {
console.log(`Index ${index}: ${value}`);
});
// entries - get index-value pairs
console.log("\nUsing entries:");
for (const [index, value] of data.entries()) {
console.log(`[${index}] = ${value}`);
}
// values - get just values
console.log("\nUsing values:");
for (const value of data.values()) {
console.log(value);
}
Output:
Using forEach:
Index 0: 1
Index 1: 2
Index 2: 3
Index 3: 4
Index 4: 5