Repository: penpot/penpot
File: frontend/src/app/main/ui/workspace/sidebar/assets/components.cljs (ClojureScript)

Code before:

        [:> cmm/component-item-thumbnail*
         {:file-id file-id
          :class (stl/css-case :thumbnail true
                               :asset-list-thumbnail (not listing-thumbs?))
          :root-shape root-shape
          :component component
          :container container
          :is-hidden (not visible?)}]
        (when (ctc/is-variant? component)
          [:span {:class (stl/css-case :variant-mark-cell listing-thumbs? :variant-mark true :component-icon true)
                  :title (tr "workspace.assets.components.num-variants" num-variants)} i/variant])])]))


(defn- count-leaves
  "Counts the total number of leaf elements in a nested map structure.
     A leaf element is considered any element inside a vector."
  [m]
  (reduce
   (fn [acc v]
     (cond
       (map? v) (+ acc (count-leaves v))
       (vector? v) (+ acc (count v))
       :else acc))
   0
   (vals m)))

(mf/defc components-group
  {::mf/wrap-props false}
  [{:keys [file-id prefix groups open-groups force-open? renaming listing-thumbs? selected on-asset-click
           on-drag-start do-rename cancel-rename on-rename-group on-group on-ungroup on-context-menu
           selected-full is-local count-variants on-group-combine-variants]}]

  (let [group-open?    (if (false? (get open-groups prefix)) ;; if the user has closed it specifically, respect that
                         false
                         (or ^boolean force-open?
                             ^boolean (get open-groups prefix (if (= prefix "") true false))))
        dragging*      (mf/use-state false)
        dragging?      (deref dragging*)


        selected-paths (mf/with-memo [selected-full]
                         (into #{}
                               (comp (map :path) (d/nilv ""))
                               selected-full))

        components     (not-empty (get groups "" []))
        can-combine?   (and is-local
                            (> (count-leaves groups) 1)
                            (not-any? ctc/is-variant? components)
                            (apply = (map :main-instance-page components)))

Review comment (refactor):
I suggest a minor change for this function: use reduce-kv instead of reduce + vals.

```clojure
(defn- count-leaves
  "Counts the total number of leaf elements in a nested map structure.
     A leaf element is considered any element inside a vector."
  [m]
  (reduce-kv (fn [acc _ v]
               (cond
                 (map? v) (+ acc (count-leaves v))
                 (vector? v) (+ acc (count v))
                 :else acc))
             0
             m))
```

The problem is not the reduce, the "problem" is vals, knowning how it is implemented in the majority of languages it generates allocation of KeyEntry or similar classs for each entry, for then: extract the value. In contrast, reduce-kv performs an internal reduce over structure without creating that sequence of KeyEntry'es.

Code after:

        [:> cmm/component-item-thumbnail*
         {:file-id file-id
          :class (stl/css-case :thumbnail true
                               :asset-list-thumbnail (not listing-thumbs?))
          :root-shape root-shape
          :component component
          :container container
          :is-hidden (not visible?)}]
        (when (ctc/is-variant? component)
          [:span {:class (stl/css-case :variant-mark-cell listing-thumbs? :variant-mark true :component-icon true)
                  :title (tr "workspace.assets.components.num-variants" num-variants)} i/variant])])]))


(defn- count-leaves
  "Counts the total number of leaf elements in a nested map structure.
     A leaf element is considered any element inside a vector."
  [m]
  (reduce-kv (fn [acc _ v]
               (cond
                 (map? v) (+ acc (count-leaves v))
                 (vector? v) (+ acc (count v))
                 :else acc))
             0
             m))

(mf/defc components-group
  {::mf/wrap-props false}
  [{:keys [file-id prefix groups open-groups force-open? renaming listing-thumbs? selected on-asset-click
           on-drag-start do-rename cancel-rename on-rename-group on-group on-ungroup on-context-menu
           selected-full is-local count-variants on-group-combine-variants]}]

  (let [group-open?    (if (false? (get open-groups prefix)) ;; if the user has closed it specifically, respect that
                         false
                         (or ^boolean force-open?
                             ^boolean (get open-groups prefix (if (= prefix "") true false))))
        dragging*      (mf/use-state false)
        dragging?      (deref dragging*)


        selected-paths (mf/with-memo [selected-full]
                         (into #{}
                               (comp (map :path) (d/nilv ""))
                               selected-full))

        components     (not-empty (get groups "" []))
        can-combine?   (mf/with-memo [is-local groups components]
                         (and is-local
                              (> (count-leaves groups) 1)
                              (not-any? ctc/is-variant? components)
                              (apply = (map :main-instance-page components))))