Skip to content

Commit

Permalink
Optimize the execution time of the unique method (#39822)
Browse files Browse the repository at this point in the history
* Optimize the execution time of the unique method

When passing a large array, the unique method is very slow. 
This modification can increase the speed by hundreds of times, 
and it is also suitable for most scenarios.

* Optimize the execution time of the unique method

* Update Collection.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
tonylevid and taylorotwell committed Nov 30, 2021
1 parent 513d214 commit 48a53be
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/Collections/Collection.php
Expand Up @@ -1454,6 +1454,10 @@ public function undot()
*/
public function unique($key = null, $strict = false)
{
if (is_null($key) && $strict === false) {
return new static(array_unique($this->items, SORT_REGULAR));
}

$callback = $this->valueRetriever($key);

$exists = [];
Expand Down

0 comments on commit 48a53be

Please sign in to comment.