This library supplies a single function, fselect-keys, an alternative to Clojure's core select-keys that performs 5–20%
faster on hashmaps containing millions of entries.
[com.sagevisuals/see-sharp "0-SNAPSHOT0"]
com.sagevisuals/see-sharp {:mvn/version "0-SNAPSHOT0"}
(require '[see-sharp.core :refer [fselect-keys]])
Feel free to directly copy-paste the code (license terms apply).
Clojure's select-keys is implemented with a
recursive first/next idiom. Could an alternative implementation using some other standard Clojure idiom perform measurably better across a wide
range of inputs?
Yes, an idiomatic variant composed of
reduce, conj!, and transients performs 5–20%
faster than Clojure's select-keys on hashmaps containing up to one-million entries.
Here's how we use fselect-keys. We supply a hashmap and a key sequence.
(fselect-keys {:a 11, :b 22, :c 33, :d 44, :e 55} [:a :c :e])
;; => {:a 11, :c 33, :e 55}
Exactly like select-keys, fselect-keys yields a hashmap with only those entries, but returns a smidge
faster.
More examples.
It probably only makes sense to use this variant if we're in some unusually performance-sensitive context where 5–20% is a significant. Otherwise, stick with Clojure's version.
If you ever think to yourself
I've got this totally awesome idea on how to rewrite some-function,
Beware that much later you'll likely realize
Good grief! Proving thatsome-function is faster takes a ton of time and grinding effort. And ultimately, it's merely 5%
faster. And only on synthetic benchmarks.
Clojure's core select-keys. No dependencies. Widely-used and battle-tested.
Ben Sless' keys. Select and rename keys as fast as possible with idiomatic Clojure.
A sequence of keys to be extracted from a hashmap. Keys need not be keywords; they may be any valid Clojure value. A vector is convenient for manually supplying.
This program and the accompanying materials are made available under the terms of the MIT License.
Can you improve this documentation?Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |