Skip to content

Instantly share code, notes, and snippets.

@tonsky
tonsky / gist:9da339f87113e1f1a395
Last active August 29, 2015 14:22
DataScript 0.11.4 perftests (clj/cljs comparison)

CLJ/CLJS perf comparison on DataScript 0.11.4

CLJ

test: q, method: d/q1, size: 100
{:unit μs, :min 61.923, :median 76.887, :max 161.738}

test: q, method: d/q2, size: 100
{:unit μs, :min 200.32, :median 213.523, :max 251.318}

@tonsky
tonsky / user.clj
Last active September 16, 2015 14:08
Querying DataScript database for entities with 3+ tags
(require '[datascript :as d])
(defn count-vals [db e a]
(count (d/datoms db :eavt e a)))
(let [db (-> (d/empty-db {:link/tags {:db/cardinality :db.cardinality/many}})
(d/db-with [[:db/add 1 :link/tags "a"]
[:db/add 1 :link/tags "b"]
[:db/add 1 :link/tags "c"]
[:db/add 2 :link/tags "x"]
@tonsky
tonsky / gist:0e90dde5b1cea02c2de0dbf1377bb41c
Created April 18, 2016 19:22
Rum 0.8.0 vs Hiccup 1.0.5
[~/Dropbox/ws/rum] lein perf
--- Testing page1.html (1 kB) ---
Evaluation count : 4656 in 6 samples of 776 calls.
Execution time mean : 127.518354 µs
Execution time std-deviation : 3.729988 µs
Execution time lower quantile : 124.490218 µs ( 2.5%)
Execution time upper quantile : 132.649851 µs (97.5%)
Overhead used : 2.051537 ns
@tonsky
tonsky / uploader.cljs
Last active September 16, 2016 10:13
Rum file uploader
(ns uploader
(:require
[clojure.string :as str]
[goog.dom :as gdom]
[goog.userAgent :as ua]
[rum.core :as rum]))
(defonce supports-dragndrop?
(let [el (js/document.createElement "div")]
(require '[criterium.core :as c])
(defn new_=
"Equality. Returns true if x equals y, false if not. Same as
Java x.equals(y) except it also works for nil, and compares
numbers and collections in a type-independent manner. Clojure's immutable data
structures define equals() (and thus =) as a value, not an identity,
comparison."
{:inline (fn [x y] `(. clojure.lang.Util equiv ~x ~y))
:inline-arities #{2}
@tonsky
tonsky / xmas.clj
Created December 19, 2016 10:15
Xmas decoration with AnyBar and Clojure
(import '[java.net InetAddress DatagramPacket DatagramSocket])
(defonce socket (DatagramSocket.))
(defn send-message [text port]
(let [host (InetAddress/getLocalHost)
packet (DatagramPacket. (.getBytes text) (.length text) host port)]
(.send socket packet)))
(require '[criterium.core :as c])
(defn format-time [estimate]
(let [mean (first estimate)
[factor unit] (c/scale-time mean)]
(c/format-value mean factor unit)))
(defmacro race [body1 body2]
`(let [_# (assert (= ~body1 ~body2))
(defn race
"Runs two fns in tight loops in two parallel threads for 1 second"
[publish-fn check-fn]
(println "---")
(let [*run? (volatile! true)
*failures (volatile! 0)
thread! #(future
(loop [i 0]
(% i)
(if @*run? (recur (inc i)) i)))
(defn quick-benchmark [fun]
(let [t0 (js/performance.now)]
(loop []
(dotimes [_ 10] (fun))
(when (< (- (js/performance.now) t0) 1000) (recur)))
(let [t0 (js/performance.now)
steps (loop [steps 0]
(dotimes [_ 10] (fun))
(if (< (- (js/performance.now) t0) 5000)
(recur (+ steps 10))
(defn digits [x]
(loop [r x, digits []]
(if (== 0 r)
digits
(recur (quot r 10) (conj digits (rem r 10))))))
(defn double-odd-digits [digits]
(map-indexed (fn [i d] (if (odd? i) (* 2 d) d)) digits))
(defn valid? [x]