Public
Edited
Sep 7, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
class Leaky {

constructor({size = 1024} = {}) {
reset; /* delete this and mannualy rerun cell to observe effects */
this.data = new Uint8Array(size)
}
insert(uint,val = 1) {
this.data[uint] = val;
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
class Container {
constructor({size = 1024} = {}) {
times;
this.data = new Uint8Array(size)
}
insert(uint) {
this.data[uint] = 1
}

}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
class DataClass {

constructor({size = 1024,runs = 0} = {}) {
reuse; // delete this and rerun manually to observe effects

this.data = new Uint8Array(size)
this.size = size;
this.runs = runs;
}

insert(int,val = 1) {
this.data[int] = val;
}

reuse(Class){
this.runs++
return this
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
class HoistClass {

constructor({size = 1024,init,runs = -1} = {}){
rerun; // delete this and run manually to observe effects
this.data = init ? init : new Uint8Array(size)
this.size = size;
this.runs = runs + 1

}
insert(int, val = 1){
this.data[int] = val;
}
}
Insert cell
Insert cell
Insert cell
recycled = {

let t1 = performance.now()
let size = 25000000;
let instance = this ? this.instance.reuse(Recycler) : new Recycler({size})

// first run
if(instance.runs == 0) {
for (let i = 0; i < size; ++i) {
instance.insert(i)
}
}
// secondary runs
else {
for (let i = 0; i < size; ++i) {
instance.insert(i,1+instance.runs)
}
}
return {
instance,
time: performance.now()-t1,
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
class Recycler {

// to prevent duplicate this.prop construction
// evaluate desired properties to undefined
// based on init condition
constructor({size = 1024, runs = 0, init = true}={}) {
// delete this and run manually to observe effects
refresh;

// use below pattern for each property you want to construct once
this.data = init ? new Uint8Array(size) : undefined
this.runs = runs
}
insert(int,val = 1) {
this.data[int] = val
}

// uncomment and rerun to add newMethod to reused Class
// newMethod(val) {
// console.log('New method says: ' + val)
// }
reuse(Class) {

// method that instantiates and copies methods and properties
// from a given Class to a prior Class instance
// without overwriting stateful properties

let stale = this; // retrieve previous Class instance
let fresh = new Class({init:false}); // set new Class

// retrieve old and new context
let [ oldFuncs , oldProps ] = Class.getContext(stale);
let [ newFuncs , newProps ] = Class.getContext(fresh);

// deconstruct fresh and stale Class contexts
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) ];

// refresh properties
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);

// refresh methods
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) {

// global utility to extract methods
// and properties from a given 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]
}
}
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more