Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

* 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...
commit 0708e91dcaa117bb7b7a5a4a44c391b6a57c62c6 1 parent af3ec4b
authored
11  benchmark/cljs/benchmark_runner.cljs
@@ -6,6 +6,17 @@
6 6
 
7 7
 (set! *print-fn* js/print)
8 8
 
  9
+(println ";; array-reduce & ci-reduce")
  10
+(def arr (let [arr (array)]
  11
+           (dotimes [i 1000000]
  12
+             (.push arr i))
  13
+           arr))
  14
+(defn sum [a b] (+ a b))
  15
+(simple-benchmark [coll (seq arr)] (ci-reduce coll + 0) 1)
  16
+(simple-benchmark [coll (seq arr)] (ci-reduce coll sum 0) 1)
  17
+(simple-benchmark [coll arr] (array-reduce coll + 0) 1)
  18
+(simple-benchmark [coll arr] (array-reduce coll sum 0) 1)
  19
+
9 20
 (println ";;; instance?")
10 21
 ;; WARNING: will get compiled away under advanced
11 22
 (simple-benchmark [coll []] (instance? PersistentVector coll) 1000000)
30  src/cljs/cljs/core.cljs
@@ -429,6 +429,34 @@ reduces them without incurring seq initialization"
429 429
                (recur nval (inc n))))
430 430
            val))))
431 431
 
  432
+(defn- array-reduce
  433
+  ([arr f]
  434
+     (if (zero? (alength arr))
  435
+       (f)
  436
+       (loop [val (aget arr 0), n 1]
  437
+         (if (< n (alength arr))
  438
+           (let [nval (f val (aget arr n))]
  439
+             (if (reduced? nval)
  440
+               @nval
  441
+               (recur nval (inc n))))
  442
+           val))))
  443
+  ([arr f val]
  444
+     (loop [val val, n 0]
  445
+         (if (< n (alength arr))
  446
+           (let [nval (f val (aget arr n))]
  447
+             (if (reduced? nval)
  448
+               @nval
  449
+               (recur nval (inc n))))
  450
+           val)))
  451
+  ([arr f val idx]
  452
+     (loop [val val, n idx]
  453
+         (if (< n (alength arr))
  454
+           (let [nval (f val (aget arr n))]
  455
+             (if (reduced? nval)
  456
+               @nval
  457
+               (recur nval (inc n))))
  458
+           val))))
  459
+
432 460
 (declare hash-coll cons pr-str counted? RSeq)
433 461
 
434 462
 (deftype IndexedSeq [a i]
@@ -897,7 +925,7 @@ reduces them without incurring seq initialization"
897 925
   (cljs.core/undefined? x))
898 926
 
899 927
 (defn ^boolean instance? [t o]
900  
-  (js* "(~{o} != null && (~{o} instanceof ~{t} || ~{o}.constructor === ~{t} || ~{t} === Object))"))
  928
+  (js* "(~{o} instanceof ~{t})"))
901 929
 
902 930
 (defn ^boolean seq?
903 931
   "Return true if s satisfies ISeq"
9  test/cljs/cljs/core_test.cljs
@@ -1315,15 +1315,6 @@
1315 1315
   (defrecord B [x])
1316 1316
   (assert (not= (A. nil) (B. nil)))
1317 1317
 
1318  
-  (assert (instance? js/Object 1))
1319  
-  (assert (instance? js/Number 1))
1320  
-  (assert (instance? js/Object "foo"))
1321  
-  (assert (instance? js/String "foo"))
1322  
-  (assert (instance? js/Object (array)))
1323  
-  (assert (instance? js/Array (array)))
1324  
-  (assert (instance? js/Object (fn [])))
1325  
-  (assert (instance? js/Function (fn [])))
1326  
-
1327 1318
   (defprotocol IFoo (foo [this]))
1328 1319
   (assert (= (meta (with-meta (reify IFoo (foo [this] :foo)) {:foo :bar}))
1329 1320
              {:foo :bar}))

0 notes on commit 0708e91

Please sign in to comment.
Something went wrong with that request. Please try again.