There is no best answer to this question. After all, you have to start from the actual project, And you should think and make choices from the perspectives of scalability/maintenance/readability, etc., these are more important. As for memory You are just worrying about consumption and the like. Even the largest js application at present will not be so scrupulous about the memory usage of a function...
Well, let me add that if you insist on high performance, I have thought of a way:
function foo( flag ) {
foo = !!flag? function() {
return true;
}: function () {
return false;
}
return foo();
}
foo(true);
console.log( foo ); // 你可以看到最终的 foo 不再是一开始定义的样子
The role of the function is not determined until the first time it is used, and all useless logic is removed after it is determined. Lazy loading + minimal memory usage. Theoretically, multiple calls should have the best performance.
There is no best answer to this question. After all, you have to start from the actual project,
And you should think and make choices from the perspectives of scalability/maintenance/readability, etc., these are more important.
As for memory You are just worrying about consumption and the like. Even the largest js application at present will not be so scrupulous about the memory usage of a function...
Well, let me add that if you insist on high performance, I have thought of a way:
The role of the function is not determined until the first time it is used, and all useless logic is removed after it is determined.
Lazy loading + minimal memory usage.
Theoretically, multiple calls should have the best performance.
Functional programming vs. imperative programming