I'm refactoring some code and wondering what pattern is least memory intensive and easiest to read when it comes to passing constants in a recursive function.
For example, I could just pass each constant down to the next recursive function, but it isn't obvious that those params are constants:
const startFoo = (myArray, isFoo, isBar) => {
console.log(isFoo, isBar);
startFoo(myArray, isFoo, isBar);
};
Alternatively, I could have 2 functions and keep constants in the closure of the first, but I'm curious if recreating the second function every time the first is called will cause unnecessary object creation & GC:
const startFoo = (myArray, isFoo, isBar) => {
const foo = myArray => {
console.log(isFoo, isBar);
foo(myArray);
};
foo(myArray);
};
Finally, I could keep it at one function, and just cache the initial values:
const startFoo = (myArray, isFoo, isBar) => {
if (!startFoo.cache) {
startFoo.cache = {
isFoo,
isBar
}
}
const {isFoo, isBar} = startFoo.cache;
console.log(isFoo, isBar);
startFoo(myArray);
};
All 3 look like they'll be good candidates for the upcoming (here-ish) TCO so I don't see that playing into the decision, but if it does that'd be good to know as well!