iatom-adv

JavaScript performance comparison

Test case created

Info

clj (ns bigatom.atom)

(defprotocol IAtom
  (-reset! [this new-value]))

(deftype ProtoAtom [state meta validator watches]
  IEquiv
  (-equiv [o other] (identical? o other))

  IDeref
  (-deref [_] state)

  IMeta
  (-meta [_] meta)

  IPrintWithWriter
  (-pr-writer [a writer opts]
    (-write writer "#<ProtoAtom: ")
    (pr-writer state writer opts)
    (-write writer ">"))

  IWatchable
  (-notify-watches [this oldval newval]
    (doseq [[key f] watches]
      (f key this oldval newval)))
  (-add-watch [this key f]
    (set! (.-watches this) (assoc watches key f)))
  (-remove-watch [this key]
    (set! (.-watches this) (dissoc watches key)))

  IHash
  (-hash [this] (goog/getUid this))

  IAtom
  (-reset! [a new-value]
           (let [validate (.-validator a)]
             (when-not (nil? validate)
               (assert (validate new-value) "Validator rejected reference state")))
           (let [old-value (.-state a)]
             (set! (.-state a) new-value)
             (when-not (nil? (.-watches a))
               (-notify-watches a old-value new-value)))
           new-value))

(defn ^:export core-reset! [a new-value]
  (let [validate (.-validator a)]
    (when-not (nil? validate)
      (assert (validate new-value) "Validator rejected reference state"))
    (let [old-value (.-state a)]
      (set! (.-state a) new-value)
      (when-not (nil? (.-watches a))
        (-notify-watches a old-value new-value))
      new-value)))

(defn ^:export my-reset! [a new-value]
  (-reset! a new-value))

(defn ^:export my-fastpath-reset! [a new-value]
  (if (instance? Atom a)
    (let [validate (.-validator a)]
      (when-not (nil? validate)
        (assert (validate new-value) "Validator rejected reference state"))
      (let [old-value (.-state a)]
        (set! (.-state a) new-value)
        (when-not (nil? (.-watches a))
          (-notify-watches a old-value new-value))
        new-value))
    (-reset! a new-value)))

(def ^:export core-atom (atom 0))
(def ^:export my-atom (ProtoAtom. 0 nil nil nil))

;; Tests are:
(core-reset! core-atom 0)
;; (my-reset! core-atom 0) ;; no protocol definition
(my-fastpath-reset! core-atom 0)
(core-reset! my-atom 0)
(my-reset! my-atom 0)
(my-fastpath-reset! my-atom 0)

Preparation code

<script src="https://humbug-user-uploads.s3.amazonaws.com/h69xInV3qh_FlbM41h8oT6T9/bootstrap.js"></script>
 

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
(core-reset! core-atom 0)
bigatom.atom.core_reset_BANG_.call(null, bigatom.atom.core_atom, 0)
pending…
(my-fastpath-reset! core-atom 0)
bigatom.atom.my_fastpath_reset_BANG_.call(null, bigatom.atom.core_atom, 0)
pending…
(core-reset! my-atom 0)
bigatom.atom.core_reset_BANG_.call(null, bigatom.atom.my_atom, 0)
pending…
(my-reset! my-atom 0)
bigatom.atom.my_reset_BANG_.call(null, bigatom.atom.my_atom, 0)
pending…
(my-fastpath-reset! my-atom 0)
bigatom.atom.my_fastpath_reset_BANG_.call(null, bigatom.atom.my_atom, 0)
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment