For efficient memory usage of a highly used class instance (multiple calls to init
), I would like to know if memory for anything inside init
function is allocated if it returns early.
class X {
private static smth: Smth
public static init(): Smth {
// if this is evalueated to true and returned
if (this.smth) {
return this.smth;
}
// does memory allocated here (imagine there will be a big object)?
const smth: Smth = {
x: 1
}
this.smth = smth
return this.smth
}
// if for previous question answer is yes, can we avoid memory allocation for `const smth` like this?
public static getInstance(): Smth {
if (!this.smth) {
throw new Error("Not initialized");
}
return this.init();
}
}
type Smth = {
x: number
}