Is it better using variables which define a function or using a function in the first place? Furthermore, is there a difference for tree-shaking?
I have a lot of calculation (static) intensive helper classes and was wondering what the best (memory/speed) is.
Here the different ways I have in mind:
class MyClass {
readonly functionA = (v: string | number, maxDeep: number, curDeep: number = 0): string => {
if (curDeep < maxDeep) {
return this.functionA(v, maxDeep, curDeep + 1);
} else {
return "function A" + v;
}
}
static functionB(v: string | number, maxDeep: number, curDeep: number = 0): string {
if (curDeep < maxDeep) {
return MyClass.functionB(v, maxDeep, curDeep + 1);
} else {
return "function B" + v;
}
}
functionC(v: string | number, maxDeep: number, curDeep: number = 0): string {
if (curDeep < maxDeep) {
return this.functionC(v, maxDeep, curDeep + 1);
} else {
return "function C" + v;
}
}
static readonly functionD = (v: string | number, maxDeep: number, curDeep: number = 0): string => {
if (curDeep < maxDeep) {
return MyClass.functionD(v, maxDeep, curDeep + 1);
} else {
return "function D" + v;
}
}
}
I tried using JSBen for measuring a difference but the results seem to be random.