Skip to content

Commit

Permalink
* benchmark/cljs/benchmark_runner.cljs: add array-reduce, like ci-red…
Browse files Browse the repository at this point in the history
…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
David Nolen authored and David Nolen committed May 28, 2012
1 parent af3ec4b commit 0708e91
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
11 changes: 11 additions & 0 deletions benchmark/cljs/benchmark_runner.cljs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@


(set! *print-fn* js/print) (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?") (println ";;; instance?")
;; WARNING: will get compiled away under advanced ;; WARNING: will get compiled away under advanced
(simple-benchmark [coll []] (instance? PersistentVector coll) 1000000) (simple-benchmark [coll []] (instance? PersistentVector coll) 1000000)
Expand Down
30 changes: 29 additions & 1 deletion src/cljs/cljs/core.cljs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -429,6 +429,34 @@ reduces them without incurring seq initialization"
(recur nval (inc n)))) (recur nval (inc n))))
val)))) 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) (declare hash-coll cons pr-str counted? RSeq)


(deftype IndexedSeq [a i] (deftype IndexedSeq [a i]
Expand Down Expand Up @@ -897,7 +925,7 @@ reduces them without incurring seq initialization"
(cljs.core/undefined? x)) (cljs.core/undefined? x))


(defn ^boolean instance? [t o] (defn ^boolean instance? [t o]
(js* "(~{o} != null && (~{o} instanceof ~{t} || ~{o}.constructor === ~{t} || ~{t} === Object))")) (js* "(~{o} instanceof ~{t})"))


(defn ^boolean seq? (defn ^boolean seq?
"Return true if s satisfies ISeq" "Return true if s satisfies ISeq"
Expand Down
9 changes: 0 additions & 9 deletions test/cljs/cljs/core_test.cljs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1315,15 +1315,6 @@
(defrecord B [x]) (defrecord B [x])
(assert (not= (A. nil) (B. nil))) (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])) (defprotocol IFoo (foo [this]))
(assert (= (meta (with-meta (reify IFoo (foo [this] :foo)) {:foo :bar})) (assert (= (meta (with-meta (reify IFoo (foo [this] :foo)) {:foo :bar}))
{:foo :bar})) {:foo :bar}))
Expand Down

0 comments on commit 0708e91

Please sign in to comment.