ECMAScript
05. Precision Math & Modern Maps
Eliminate IEEE-754 floating-point accumulation bugs and remove dictionary fallback boilerplate using native ES2026 collection primitives.
math-and-maps.ts
// 1. Exact IEEE 754 Summation without roundoff error
const values = [0.1, 0.2, 0.3, -0.6];
const standardSum = values.reduce((acc, curr) => acc + curr, 0); // 5.551115123125783e-17 (Bug!)
const exactSum = Math.sumPrecise(values); // 0.0 (Exact accumulator)
// 2. Map.prototype.getOrInsert Key Memoization
const userCache = new Map<string, { data: string; fetchedAt: number }>();
function getUserData(userId: string) {
return userCache.getOrInsert(userId, {
data: `User record for ${userId}`,
fetchedAt: Date.now()
});
}Key Mechanics
Math.sumPrecise(): Native IEEE-754 summation avoiding binary floating-point roundoff errors without external BigNumber overhead.Map.prototype.getOrInsert(): Eliminates boilerplate lookup-and-fallback logic by lazily computing and inserting default values in a single call.
IEEE-754 Precision Accumulator Float Precision
Active Array: [0.1, 0.2, 0.3]
Standard reduce(+)
0.6000000000000001
Math.sumPrecise()
0.6
Map.prototype.getOrInsert Memoization Atomic Operations
user:100
Hits: 4 ยท Pre-warmed
โบ Cache pre-warmed with key [user:100]