Permalink
Browse files

* benchmark/cljs/benchmark_runner.cljs: add array-reduce, like ci-red…

…uce but for working directly with arrays. add ci-reduce & array-reduce benchmarks. instance? is now just JS instanceof, reduce ops now faster across the board.
  • Loading branch information...
1 parent af3ec4b commit 0708e91dcaa117bb7b7a5a4a44c391b6a57c62c6 David Nolen committed May 28, 2012
Showing with 40 additions and 10 deletions.
  1. +11 −0 benchmark/cljs/benchmark_runner.cljs
  2. +29 −1 src/cljs/cljs/core.cljs
  3. +0 −9 test/cljs/cljs/core_test.cljs
@@ -6,6 +6,17 @@
(set! *print-fn* js/print)
+(println ";; array-reduce & ci-reduce")
+(def arr (let [arr (array)]
+ (dotimes [i 1000000]
+ (.push arr i))
+ arr))
+(defn sum [a b] (+ a b))
+(simple-benchmark [coll (seq arr)] (ci-reduce coll + 0) 1)
+(simple-benchmark [coll (seq arr)] (ci-reduce coll sum 0) 1)
+(simple-benchmark [coll arr] (array-reduce coll + 0) 1)
+(simple-benchmark [coll arr] (array-reduce coll sum 0) 1)
+
(println ";;; instance?")
;; WARNING: will get compiled away under advanced
(simple-benchmark [coll []] (instance? PersistentVector coll) 1000000)
@@ -429,6 +429,34 @@ reduces them without incurring seq initialization"
(recur nval (inc n))))
val))))
+(defn- array-reduce
+ ([arr f]
+ (if (zero? (alength arr))
+ (f)
+ (loop [val (aget arr 0), n 1]
+ (if (< n (alength arr))
+ (let [nval (f val (aget arr n))]
+ (if (reduced? nval)
+ @nval
+ (recur nval (inc n))))
+ val))))
+ ([arr f val]
+ (loop [val val, n 0]
+ (if (< n (alength arr))
+ (let [nval (f val (aget arr n))]
+ (if (reduced? nval)
+ @nval
+ (recur nval (inc n))))
+ val)))
+ ([arr f val idx]
+ (loop [val val, n idx]
+ (if (< n (alength arr))
+ (let [nval (f val (aget arr n))]
+ (if (reduced? nval)
+ @nval
+ (recur nval (inc n))))
+ val))))
+
(declare hash-coll cons pr-str counted? RSeq)
(deftype IndexedSeq [a i]
@@ -897,7 +925,7 @@ reduces them without incurring seq initialization"
(cljs.core/undefined? x))
(defn ^boolean instance? [t o]
- (js* "(~{o} != null && (~{o} instanceof ~{t} || ~{o}.constructor === ~{t} || ~{t} === Object))"))
+ (js* "(~{o} instanceof ~{t})"))
(defn ^boolean seq?
"Return true if s satisfies ISeq"
@@ -1315,15 +1315,6 @@
(defrecord B [x])
(assert (not= (A. nil) (B. nil)))
- (assert (instance? js/Object 1))
- (assert (instance? js/Number 1))
- (assert (instance? js/Object "foo"))
- (assert (instance? js/String "foo"))
- (assert (instance? js/Object (array)))
- (assert (instance? js/Array (array)))
- (assert (instance? js/Object (fn [])))
- (assert (instance? js/Function (fn [])))
-
(defprotocol IFoo (foo [this]))
(assert (= (meta (with-meta (reify IFoo (foo [this] :foo)) {:foo :bar}))
{:foo :bar}))

0 comments on commit 0708e91

Please sign in to comment.