class Recycler {
constructor({size = 1024, runs = 0, init = true}={}) {
refresh;
this.data = init ? new Uint8Array(size) : undefined
this.runs = runs
}
insert(int,val = 1) {
this.data[int] = val
}
reuse(Class) {
let stale = this;
let fresh = new Class({init:false});
let [ oldFuncs , oldProps ] = Class.getContext(stale);
let [ newFuncs , newProps ] = Class.getContext(fresh);
let [ oldPropKeys, oldPropVals ]
= [ Object.keys(oldProps) , Object.values(oldProps) ];
let [ newPropKeys, newPropVals ]
= [ Object.keys(newProps) , Object.values(newProps) ];
let [ oldFuncKeys, oldFuncVals ]
= [ Object.keys(oldFuncs) , Object.values(oldFuncs) ];
let [ newFuncKeys, newFuncVals ]
= [ Object.keys(newFuncs) , Object.values(newFuncs) ];
let removeProps = oldPropKeys.filter(prop => ! newPropKeys.includes(prop));
let updateProps = newPropKeys.filter(prop => ! oldPropKeys.includes(prop));
removeProps . forEach(prop=> delete stale[prop]);
updateProps . forEach(prop=>!stale[prop] && fresh[prop] ? stale[prop] = fresh[prop] : null);
let removeFuncs = oldFuncKeys;
let updateFuncs = newFuncKeys.map((func,i) => [func,newFuncVals[i].bind(stale)]);
removeFuncs . forEach(func=> delete stale.constructor.prototype[func]);
updateFuncs . forEach(([name,func])=> Object.defineProperty(stale.constructor.prototype, name, { value:func, configurable:true}));
this.runs++
return this
}
static getContext(Class) {
let { constructor, ... funcs } =
( Object.fromEntries ( Object.getOwnPropertyNames
( Object.getPrototypeOf ( Class )).map( key=> [key,Class[key]])));
let { ... props } =
( Object.fromEntries ( Object.getOwnPropertyNames
( Class ).map( key=> [key,Class[key]])));
return [funcs,props]
}
}