Doctrine Common
  1. Doctrine Common
  2. DCOM-59

ArrayCollection contains function - switch from in_array to foreach

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 2.1
    • Fix Version/s: 2.2
    • Component/s: Collections
    • Labels:
      None

      Description

      The function contains uses in_array to determine if an element provided as parameter is found in the array $this->_elements, and performs a strict comparison.
      There might be a way to improve the performances of this function by using a foreach + if

      In attachement you can find a small bench file to show the differences between in_array and foreach on object search.
      You might want to run the bench with a doctrine Object to verify if this changes anything to the result.
      For simple Objects, foreach is much faster when the array has less than 30k elements. Over 30k the difference flattens, and might revert around 50k (meaning that in_array might start being faster when the array has more than 50k entries).

      Maybe we could provide two functions, one for smaller than 40-50k items (using foreach), and one for bigger arrays (using in_array) ?

        Activity

        Hide
        Guilherme Blanco added a comment -

        I created more test cases and indeed foreach is around 40% faster than in_array. I applied the change in our suite, this should be available in new versions of Doctrine Common.

        Show
        Guilherme Blanco added a comment - I created more test cases and indeed foreach is around 40% faster than in_array. I applied the change in our suite, this should be available in new versions of Doctrine Common.

          People

          • Assignee:
            Guilherme Blanco
            Reporter:
            PEM
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: