PDA

View Full Version : Dynamic method for speed optimisation



kung foo man
24th November 2018, 11:53
I am currently porting some JavaScript code and I don't know the best way to port over such 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/blob/583883cc5111fabb64f2f57177f715cd6fa777fc/src/math/mat4.js#L418-L452

The three Vec3 are allocated once to prevent garbage collection.