Permalink
Browse files

Use loop when possible; it's fast.

  • Loading branch information...
1 parent 032fbde commit 0f6cc0db91ee1cb727df0d913767120f88c06299 @lynaghk committed May 22, 2012
Showing with 42 additions and 0 deletions.
  1. +37 −0 src/cljs/microbenchmarks/dom.cljs
  2. +5 −0 src/cljs/microbenchmarks/seqs.cljs
@@ -6,6 +6,10 @@
(def $test (.querySelector js/document "#test"))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;Test passthrough google closure
+
(profile {:group "dom" :n 10000}
(let [$el (.createElementNS js/document
"http://www.w3.org/1999/xhtml"
@@ -21,3 +25,36 @@
(.removeChild $test $el)))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;Test looping over children
+
+(dotimes [i 10000]
+ (let [$el (.createElementNS js/document
+ "http://www.w3.org/1999/xhtml"
+ "p")]
+ (.appendChild $test $el)))
+
+(extend-type js/NodeList
+ ISeqable
+ (-seq [array] (array-seq array 0)))
+
+(extend-type js/HTMLCollection
+ ISeqable
+ (-seq [array] (array-seq array 0)))
+
+(profile {:group "dom"}
+ (doseq [$d (.-children $test)]))
+
+(profile {:group "dom"}
+ (let [$children (.-children $test)
+ n (.-length $children)]
+ (loop [idx 0]
+ (aget $children idx)
+ (when (< idx n)
+ (recur (inc idx))))))
+
+
+(set! (.-innerHTML $test) "")
+
+
@@ -4,6 +4,11 @@
(profile "count up"
(doseq [i (range 100000)]))
+(profile "count up"
+ (loop [i 0]
+ (when (< i 100000)
+ (recur (inc i)))))
+
(profile "map inc"
(doall (map inc (range 100000))))

0 comments on commit 0f6cc0d

Please sign in to comment.