Does JavaScript treat:
array.filter(x => {
const test = [1, 2, 3]
return test.includes(x);
});
the same as:
const test = [1, 2, 3]
array.filter(x => {
return test.includes(x);
});
or will the const cause extra work on each evaluation in the former?
With the first, you're declaring an array only once.
With the second, you're declaring an array every time. Memory has to be allocated for each array and then garbage collected.
The first is more efficient.
Note that const does not mean the value is constant (except with primitives), const means that the variable reference can't change (for example, you can call .push on test on the inside array, even with const, but you can't reassign test)
However it is possible that your engine could optimize for this