From 48a53be4418d6d0f8eee443e272158f1d2af832b Mon Sep 17 00:00:00 2001 From: tonylevid Date: Tue, 30 Nov 2021 22:29:03 +0800 Subject: [PATCH] Optimize the execution time of the unique method (#39822) * 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 --- src/Illuminate/Collections/Collection.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 33b5b5de78f6..622651991194 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -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 = [];