On Friday 28 November 2014 14:51:55 Ferenc Kovacs wrote:
>
> I also used spl_object_hash() in the past when traversing/custom
> serializing object structures which can have infinite recursions 
between
> objects, but even that could be simply solved by storing the already
> traversed objects in an array (as assigning the objects into another
> variable doesn't have much overhead) and checking with in_array.

Checking with in_array is O(N) while an array keyed on 
spl_object_hash() with the values being the objects, is stable wrt. hash 
string vs. object lifetime, and is O(1) on checking for key existence.

The performance difference will become apparent when you have more 
than a handful of objects there.

BTW, the two or three places where I use spl_object_hash(), in this way, 
are exactly your use case: as an "already visited" cache during traversal 
of somewhat arbitrary input structure (logging / dumping).

best regards
  Patrick

Reply via email to