You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Why not save a bit of memory by using proxies with caching to lighten the footprint of methods? Also, while we're at it, the length is a bit useless, and could probably be shed. (Most of the time functions depend on lengths, they also have a facility to specify it explicitly.)
Example implementation
constmethodData=newWeakMap()constmethodCache=newMap()consthasOwn=Object.prototype.hasOwnPropertyconstproxyTarget=Object.create(null)Object.defineProperty(proxyTarget,Symbol.toStringTag,{configurable: false,enumerable: false,writable: false,value: "Module Proxy",})functioncompileMethod(name){letmemo=methodCache.get(name)if(memo==null){methodCache.set(name,memo=/** @this */function(...args){const{module, options}=methodData.get(this)returnmodule.pool.invoke({module, options, name},args.length ? args : undefined)})}returnmemo}exports.ModuleWrap=classModuleWrap{constructor(pool,name,methods){this.pool=poolthis.name=namethis.methods=methodsthis.proxy=this.apply(proxyTarget,undefined,[])}apply(target,thisArg,[options]){if(options==null||typeofoptions!=="object"){thrownewTypeError("`options` must be an object")}constproxy=newProxy(target,this)methodData.set(proxy,{module: this, options})returnproxy}get(target,key,receiver){returnhasOwn.call(this.methods,key)
? compileMethod(key)
: Reflect.get(target,key,receiver)}has(target,key){returnhasOwn.call(this.methods,key)||Reflect.has(target,key)}set(){returnfalse}defineProperty(){returnfalse}deleteProperty(){returnfalse}setPrototypeOf(){returnfalse}preventExtensions(){returnfalse}}
The text was updated successfully, but these errors were encountered:
Why not save a bit of memory by using proxies with caching to lighten the footprint of methods? Also, while we're at it, the length is a bit useless, and could probably be shed. (Most of the time functions depend on lengths, they also have a facility to specify it explicitly.)
Example implementation
The text was updated successfully, but these errors were encountered: