@himorishige/noren-core
Version:
Core PII detection, masking, and tokenization library built on Web Standards
53 lines (52 loc) • 1.6 kB
JavaScript
// Optimized hit pool with minimal overhead
export class HitPool {
pool = [];
maxSize = 20; // Smaller pool for better performance
clearCounter = 0;
acquire(type, start, end, value, risk, priority) {
const hit = this.pool.pop();
if (hit) {
// Reuse existing object
hit.type = type;
hit.start = start;
hit.end = end;
hit.value = value;
hit.risk = risk;
hit.priority = priority;
return hit;
}
// Create new object if pool is empty
return { type, start, end, value, risk, priority };
}
releaseOne(hit) {
this.release([hit]);
}
release(hits) {
// Return hits to pool for reuse
for (const hit of hits) {
if (this.pool.length < this.maxSize) {
// Simple cleanup - let JS GC handle the rest
hit.value = '';
hit.priority = undefined;
hit.confidence = undefined;
hit.reasons = undefined;
hit.features = undefined;
this.pool.push(hit);
}
}
// Periodic cleanup every 100 releases
if (++this.clearCounter >= 100) {
this.pool.length = 0;
this.clearCounter = 0;
}
}
releaseRange(hits, count) {
const toRelease = hits.slice(0, Math.min(count, hits.length));
this.release(toRelease);
}
clear() {
this.pool.length = 0;
this.clearCounter = 0;
}
}
export const hitPool = new HitPool();