【发布时间】:2021-07-27 06:38:17
【问题描述】:
是使用定义函数的变量还是首先使用函数更好? 此外,摇树有区别吗?
我有很多计算(静态)密集型辅助类,我想知道最好的(内存/速度)是什么。
这是我想到的不同方式:
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;
}
}
}
我尝试使用JSBen 来测量差异,但结果似乎是随机的。
【问题讨论】:
标签: javascript typescript function class tree-shaking