Jump to content

Performance penalty when comparing $page object instead of $page->id


slkwrm
 Share

Recommended Posts

Hey guys. Does

<?php $p === $page

have bigger overhead then

<?php p->id === $page->id

Is there any difference in performance?

I don't think so (if you remember to use === instead of ==). But not sure about this, Ryan probably knows more. Here is some information, but not much about performance: http://php.net/manual/en/language.oop5.object-comparison.php

Link to comment
Share on other sites

Regarding whether to use "===" or "==": if you are comparing Page object to object, you would want to use the "===" rather than "==". That tells PHP to check that they are in fact the same instance (i.e. occupying the same block of memory). There may be a slight performance benefit to using "===" to compare object-to-object rather than comparing ID because $page->id triggers some action in the class, where as "===" is likely handled all in PHP's core code (in C).

Regardless, I prefer to compare the ID. It doesn't matter if you use "===" or "==" when comparing the ID. There is also a side benefit in comparing the ID because there is a possibility that you could have two instances of the same page in memory. PW tries to prevent this from happening by keeping a memory cache of everything that gets loaded. But if you have a $page in your code's scope and not in PW's scope (due to some action that clears PW's memory cache), and the page gets loaded again from a $pages->find (for example), then it's feasible you could have two instances of the same page... yours and PW's. In such a case, comparing $p === $page would return false, whereas $p->id == $page->id would return true. For this reason, I think it may be better to compare the ID.

Actions that clear the cache are: saving or deleting pages. If your block of code doesn't do any page saving or deleting (or manual cache clearing)… or calling anything that does, then comparing object-to-object with "===" is a fine way to go. But if you don't want to have to think about such things, then maybe better to just compare the IDs.

Link to comment
Share on other sites

Apeisa, Ryan, thank you. Now this topic is clear for me. Btw, wanted to express my appriciation to Ryan for such a clear code with comprehensive comments. PW source code is a really good read ;)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...