Make native components a little faster by optimizing common cases

This commit is contained in:
Dan Holmsand 2014-03-16 08:29:35 +01:00
parent 56e11f99c7
commit f84369a53a
1 changed files with 31 additions and 26 deletions

View File

@ -32,12 +32,14 @@
(util/clj-ifn? x))) (util/clj-ifn? x)))
(defn to-js-val [v] (defn to-js-val [v]
(if-not (ifn? v) (cond
v (string? v) v
(cond (keyword? v) (name v) (number? v) v
(symbol? v) (str v) (keyword? v) (name v)
(coll? v) (clj->js v) (symbol? v) (str v)
:else (fn [& args] (apply v args))))) (coll? v) (clj->js v)
(ifn? v) (fn [& args] (apply v args))
:else v))
(defn undash-prop-name [n] (defn undash-prop-name [n]
(or (attr-aliases n) (or (attr-aliases n)
@ -49,23 +51,24 @@
(def cached-prop-name (util/memoize-1 undash-prop-name)) (def cached-prop-name (util/memoize-1 undash-prop-name))
(def cached-style-name (util/memoize-1 util/dash-to-camel)) (def cached-style-name (util/memoize-1 util/dash-to-camel))
(defn convert-prop-value [val] (defn convert-prop-value [x]
(if (map? val) (cond (string? x) x
(reduce-kv (fn [res k v] (number? x) x
(doto res (map? x) (reduce-kv (fn [o k v]
(aset (cached-prop-name k) (doto o
(to-js-val v)))) (aset (cached-prop-name k)
#js {} val) (to-js-val v))))
(to-js-val val))) #js {} x)
:else (to-js-val x)))
(defn set-id-class [props [id class]] (defn set-id-class [props [id class]]
(let [pid (get. props :id)] (let [pid (get. props :id)]
(set. props :id (if-not (nil? pid) pid id)) (set. props :id (if-not (nil? pid) pid id))
(when-not (nil? class) (when-not (nil? class)
(set. props :className (let [old (get. props :className)] (let [old (get. props :className)]
(if-not (nil? old) (set. props :className (if-not (nil? old)
(str class " " old) (str class " " old)
class)))))) class))))))
(defn convert-props [props id-class] (defn convert-props [props id-class]
(cond (cond
@ -228,7 +231,8 @@
(defn as-component (defn as-component
([x] (as-component x 0)) ([x] (as-component x 0))
([x level] ([x level]
(cond (vector? x) (vec-to-comp x level) (cond (string? x) x
(vector? x) (vec-to-comp x level)
(seq? x) (if-not (and (dev?) (nil? ratom/*ratom-context*)) (seq? x) (if-not (and (dev?) (nil? ratom/*ratom-context*))
(expand-seq x level) (expand-seq x level)
(let [s (ratom/capture-derefed (let [s (ratom/capture-derefed
@ -250,10 +254,11 @@
a)) a))
(defn convert-args [argv first-child level] (defn convert-args [argv first-child level]
(let [a (into-array argv)] (if (== (count argv) (inc first-child))
(dotimes [i (alength a)] ;; Optimize common case of one child
(when (>= i first-child) #js [nil (as-component (nth argv first-child) level)]
(aset a i (as-component (aget a i) level)))) (reduce-kv (fn [a k v]
(when (== first-child 2) (when (>= k first-child)
(.shift a)) (.push a (as-component v level)))
a)) a)
#js [nil] argv)))