Compare props map using identical? only

That makes shouldComponentUpdate even faster, especially with big
maps/vectors as args
This commit is contained in:
Dan Holmsand 2013-12-20 19:18:02 +01:00
parent 8bd03a8b36
commit 9344edc7da
1 changed files with 29 additions and 10 deletions

View File

@ -69,6 +69,7 @@
objprops))))
(defn identical-parts [v1 v2 from]
;; Compare two vectors, from item with index "from", using identical?
(let [end (count v1)]
(loop [n from]
(if (>= n end)
@ -77,17 +78,35 @@
(recur (inc n))
false)))))
(def -not-found (js-obj))
(defn shallow-equal-maps [x y]
;; Compare two maps, using identical? on all values
(or (identical? x y)
(when (== (count x) (count y))
(reduce-kv (fn [res k v]
(let [yv (get y k -not-found)]
(if (or (identical? v yv)
;; hack to allow cloact.core/partial
(and (ifn? v) (= v yv)))
res
(reduced false))))
true x))))
(defn equal-args [v1 v2]
(let [c1 (count v1)]
(and (= (nth v1 0) (nth v2 0))
(identical? c1 (count v2))
(if (< c1 2)
true
(let [props1 (nth v1 1)]
(if (or (nil? props1) (map? props1))
(and (identical-parts v1 v2 2)
(= props1 (nth v2 1)))
(identical-parts v1 v2 1)))))))
;; Compare two "args" vectors, i.e things like [:div {:foo "bar} "baz"],
;; using identical? on all individual parts.
(or (identical? v1 v2)
(let [c1 (count v1)]
(and (= (nth v1 0) (nth v2 0)) ; may be symbol or fn
(identical? c1 (count v2))
(if (< c1 2)
true
(let [props1 (nth v1 1)]
(if (or (nil? props1) (map? props1))
(and (identical-parts v1 v2 2)
(shallow-equal-maps props1 (nth v2 1)))
(identical-parts v1 v2 1))))))))
(declare wrapper)