The part that would violate guarantees in JavaScript is not function objects being kept alive longer, but function objects which should be distinct not being so.
function foo() {
return function() { };
}
console.log(foo() === foo()); // This must log `false` in a compliant implementation
This is also a problem, IMO, in having this optimization in PHP. Anonymous functions are instances of a Closure class, which means that the `===` operator should return false for `foo() === foo()` just like it would for `new MyClass() === new MyClass()`.
But, since when has PHP ever prioritized correctness or consistency over trivial convenience? (I know it's anti-cool these days to hate on PHP, but I work with PHP all the time and it's still a terrible language even in 2026)