Skip to content

Instantly share code, notes, and snippets.

@Frozenlock
Created June 16, 2017 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Frozenlock/f88502ef5cf947cbc2d51e8d0a524d98 to your computer and use it in GitHub Desktop.
Save Frozenlock/f88502ef5cf947cbc2d51e8d0a524d98 to your computer and use it in GitHub Desktop.
;; some speed tests of functions to access nested properties in JS objects.
(def test-data (->> (into {} (for [k1 (range 10)]
[k1 (into {} (for [k2 (range 10)]
[k2 (into {} (for [k3 (range 10)]
[k3 k3]))]))]))
(clj->js)))
(defn aget-apply [o path]
(apply aget o path))
(defn oget-in [js-object path]
(reduce (fn [o p]
(goog.object/getValueByKeys o p)) js-object path))
(defn aget-reduce [o path]
(reduce (fn [o p]
(aget o p)) o path))
(defn aget-loop [o path]
(loop [cur-o o p path]
(let [result
(aget cur-o (first p))]
(if-let [new-path (seq (rest p))]
(recur result new-path)
result))))
(simple-benchmark [] (aget-apply test-data ["2" "2" "2"]) 100000) ;; --> 339msecs
(simple-benchmark [] (oget-in test-data ["2" "2" "2"]) 100000) ;; --> 208msecs
(simple-benchmark [] (aget-reduce test-data ["2" "2" "2"]) 100000) ;; --> 134msecs
(simple-benchmark [] (aget-loop test-data ["2" "2" "2"]) 100000) ;; --> 84msecs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment