The Trouble with Maps

The great thing about Clojure maps—those lovely {:key "value"} creatures—is that they are very flexible. You can use a map to associate just about any key with essentially any value. It’s this I can deal with anything flexibility that makes maps so useful and ubiquitous in Clojure programs.

But this flexibility of maps does not come for free. Part of the cost of flexibility can be measured in CPU seconds at runtime. Maps are wonderfully speedy, but the deal with anything flexibility does come with a runtime penalty. Since maps need to deal with arbitrary keys, they are a bit slower than a data structure designed to deal with just these keys. Mostly the speed penalty doesn’t matter, but sometimes—for example, when you’re ...

Get Getting Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.