It is an interesting comparison that JavaScript always ensures that different evaluations of a closure expression results in unique instances. So in general the closures will require allocations for each (unless the allocation is otherwise prevented such as via escape analysis). Of course much of the underlying data may be shared, but the identity itself will be unique.
I don't know how strict JavaScript garbage collection rules are. This was non-observable for the longest time but FinalizationRegistry now exists which makes cleanup observable. It sounds like basically no guarantees are provided when an object will be cleaned up, so presumably an implementation would be allowed to make optimizations such are proposed where for PHP.
PHP by now is very much like Java except it is dynamic.
I mean this in a positive way. You can get the highly Object Oriented architecture of Java (if you like that kind of thing) in a non-compiled, dynamic language. Very impressive.
Add to it, the usual shared nothing architecture which makes PHP amazing for scaling horizontally. PHP stopped being "bad" since PHP 7 but most developers still think of inconsistent mess that is PHP 4 when they think of PHP.
If the closure doesn't use $this (an instance of the current class) then it doesn't need to store a reference to it, which also skips the bookkeeping from the garbage collector.
It is an interesting comparison that JavaScript always ensures that different evaluations of a closure expression results in unique instances. So in general the closures will require allocations for each (unless the allocation is otherwise prevented such as via escape analysis). Of course much of the underlying data may be shared, but the identity itself will be unique.
I don't know how strict JavaScript garbage collection rules are. This was non-observable for the longest time but FinalizationRegistry now exists which makes cleanup observable. It sounds like basically no guarantees are provided when an object will be cleaned up, so presumably an implementation would be allowed to make optimizations such are proposed where for PHP.
> Non-static closures are turned into static ones if they are guaranteed not to make use of $this.
> Stateless closures, i.e. those that are static, don't capture any variables and don't declare any static variables, are cached between uses.
PHP by now is very much like Java except it is dynamic.
I mean this in a positive way. You can get the highly Object Oriented architecture of Java (if you like that kind of thing) in a non-compiled, dynamic language. Very impressive.
Add to it, the usual shared nothing architecture which makes PHP amazing for scaling horizontally. PHP stopped being "bad" since PHP 7 but most developers still think of inconsistent mess that is PHP 4 when they think of PHP.
~3% performance gains. I didn’t understood part about $this.
If the closure doesn't use $this (an instance of the current class) then it doesn't need to store a reference to it, which also skips the bookkeeping from the garbage collector.