Closed thheller closed 7 years ago
If they're not sorted then you get an unpredictable output. The same data structure can result in different output strings. This can cause a problem if you're trying to cache the output, and of course it means that the function is no longer pure.
Well the order of attributes in html is irrelevant so it should not matter? Not sure I buy the "pure" argument either as the any clojure datastructure will always seq
in the same manner thus preserving order?
It matters if the cache is not aware of the original data structure, and we're just performing a string or byte comparison to check equality. It doesn't matter if two strings of HTML are equivalent if we can't easily tell that they're equivalent.
Ensuring that the equivalent input produces equivalent output is a useful enough property that it's worth the minor cost of having an additional sort
. In many cases attributes are literal, so there's no runtime cost, and even in extreme cases the performance cost is relatively minor. For example, when dynamically rendering 1000 elements with attributes, removing the sort
results in a 5% reduction in execution time.
Thanks for the reply. I guess it is really not that important since most of it will be precompiled anyways.
I was debugging some HTML generation and ended up with an uncompiled hiccup form which went through the
hiccup.compiler/render-html
protocol. I noticed that attributes are sorted?https://github.com/weavejester/hiccup/blob/master/src/hiccup/compiler.clj#L57
Why are they sorted? The cost isn't that significant for fully compiled forms but otherwise the
apply
andsort
seem like a performance waste?