When it comes to compiler optimization in other programming languages, a similar scenario would involve pulling out certain objects from the loop to avoid creating them each time:
const arr = [1, 2, 3, 4, 5]
arr.map(num => {
const one_time = 5; // this value remains constant and can be moved outside the loop
return num * one_time;
})
I have attempted to see if the typescript compiler optimizes the code above, but it doesn't appear to do so. Perhaps there is another mechanism at play that handles it. It could also be speculated that the object creation will set the variable once and not change it.
I don't anticipate ES2015 to perform this optimization since it's an interpreted language, but I am open to being proven wrong.
If anyone has insights on how to independently determine such optimizations in the future, it would be greatly appreciated.
Thank you!