I am currently porting some JavaScript code and I don't know the best way to port over such code:
PHP Code:
function verySlowCalculationOnlyDoThisOnce() {
console.log("logging slow calculation");
return "bye";
}
Test = function(name) {
this.name = name;
}
Object.assign(Test.prototype, {
hello: function() {
console.log("hello " + this.name)
},
bye: (function() {
var byeMessage = verySlowCalculationOnlyDoThisOnce();
return function() {
return byeMessage + ", " + this.name + "!";
}
}())
})
The bye method basically prepares a part of the return value only once, every test = new Test("you") will reuse the result of the slow function.
How to rewrite such code for TypeScript?
The actual real life example is this:
https://github.com/playcanvas/engine...4.js#L418-L452
The three Vec3 are allocated once to prevent garbage collection.