ES16 Interactive Hub — ECMAScript 2025
Iterator Helpers (map/filter/take lazy chain), Set methods (union/intersection/...), Promise.try, RegExp.escape, Import Attributes (with { type: "json" }), Float16Array — toolbox functional richer + native FP16 cho ML/GPU.
Iterator Helpers
Chuyên đề cốt lõi
💡 Khái niệm tóm tắt
Bổ sung các method map/filter/take/drop/toArray/forEach/reduce trực tiếp lên Iterator.prototype — xử lý lazy chain mà không cần convert sang Array trước.
🤔 Tại sao cần tính năng này?
Trước ES16, để dùng các method như .map() / .filter() trên kết quả của generator, bạn phải convert sang Array bằng [...gen] hoặc Array.from(). Điều này MẤT TÍNH LAZY — đọc hết stream vào memory ngay cả khi chỉ cần vài phần tử đầu. Iterator Helpers cho phép gọi method trực tiếp lên iterator, vẫn giữ lazy evaluation, take(n) thực sự chỉ kéo n phần tử.
// Trước ES16 — phải convert Array, mất lazy
function* infinite() {
let i = 0;
while (true) yield i++;
}
// Cố gắng [...infinite()].map(x => x * 2).slice(0, 5) → CHẾT vì infinite
// Phải tự loop:
function take(iter, n) {
const result = [];
let count = 0;
for (const v of iter) {
if (count >= n) break;
result.push(v);
count++;
}
return result;
}
function mapIter(iter, fn) {
return (function*() {
for (const v of iter) yield fn(v);
})();
}
const first5Doubled = take(mapIter(infinite(), x => x * 2), 5);
console.log(first5Doubled); // [0, 2, 4, 6, 8]// ES16 — Iterator Helpers native, lazy chain
function* infinite() {
let i = 0;
while (true) yield i++;
}
const first5Doubled = infinite()
.map(x => x * 2)
.take(5)
.toArray();
console.log(first5Doubled); // [0, 2, 4, 6, 8]
// Filter + drop + take chain
const result = infinite()
.filter(x => x % 3 === 0) // 0, 3, 6, 9, 12, 15...
.drop(2) // bỏ 2 đầu → 6, 9, 12...
.take(3) // lấy 3 → 6, 9, 12
.toArray();
console.log(result); // [6, 9, 12]
// forEach / reduce / some / every / find cũng có
const sum = [1, 2, 3, 4, 5].values()
.map(x => x * x)
.reduce((a, b) => a + b, 0);
console.log(sum); // 55Bảng Tra Cứu Nhanh ES16
Xem nhanh cấu trúc + copy snippet
Bổ sung các method map/filter/take/drop/toArray/forEach/reduce trực tiếp lên Iterator.prototype — xử lý lazy chain mà không cần convert sang Array trước.
function* infinite() {
let i = 0;
while (true) yield i++;
...Set.prototype được bổ sung 7 method phép toán tập hợp native: union, intersection, difference, symmetricDifference, isSubsetOf, isSupersetOf, isDisjointFrom.
const a = new Set([1, 2, 3, 4]); const b = new Set([3, 4, 5, 6]); console.log([...a.union(b)]); // [1,2,3,4,5,6] ...
Wrap một function (sync hoặc async) thành Promise đồng nhất — bắt cả lỗi throw đồng bộ lẫn rejection async trong cùng một .catch().
function maybe(input) {
if (input < 0) throw new Error("Sync error!");
if (input === 0) return Promise.reject(new Error("Async error!"));
...Hàm static escape các ký tự đặc biệt của regex khỏi chuỗi (vd . * + ? ( ) [ ] { } \ ^ $ |) — tạo regex an toàn từ user input mà không lo injection.
const userInput = "1.5+ năm (2 dự án)"; const safe = RegExp.escape(userInput); console.log(safe); // chuỗi đã escape đầy đủ ...
Cú pháp `import x from "..." with { type: "json" }` chuẩn hoá việc import file non-JS (JSON, CSS, WASM...) cùng metadata cho host hiểu định dạng.
import config from './config.json' with { type: 'json' };
console.log(config.appName, config.version);
const data = await import('./data.json', { with: { type: 'json' } });
...Typed array số thực 16-bit (half-precision IEEE 754) — tiết kiệm 50% memory so với Float32, phục vụ ML/GPU/Web Neural Network/dữ liệu sensor.
const arr = new Float16Array([1.5, 2.25, 3.125]); console.log(arr.byteLength); // 6 bytes (3 × 2) — tiết kiệm 50%! console.log(arr.length); // 3 ...