Liking cljdoc? Tell your friends :D

Quick summary
Setup
API
Changelog
Usage
Performance
Alternatives
Glossary
Contact

See-sharp

A slightly faster implementation of Clojure's `select-keys`

Quick summary

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.

Setup

Leiningen/Boot

[com.sagevisuals/see-sharp "0-SNAPSHOT0"]

Clojure CLI/deps.edn

com.sagevisuals/see-sharp {:mvn/version "0-SNAPSHOT0"}

Require

(require '[see-sharp.core :refer [fselect-keys]])

Recommended

Feel free to directly copy-paste the code (license terms apply).

Motivation & usage

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.

Commentary

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.

Meta-commentary

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 that some-function is faster takes a ton of time and grinding effort. And ultimately, it's merely 5% faster. And only on synthetic benchmarks.

Alternatives

  • 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.

Glossary

key sequence

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.


License

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

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close