experimenting with a more general way of hashing a namespace, for future javascript golf. the idea is that existing browser objects can be hashed so that wasteful strings like createDocumentFragment
can be avoided entirely, and keys from several objects (such as document
, window
, String
, etc.) can be hashed into one namespace.
for example:
d = document
d.body.appendChild(d.createElement("script")).src="URL"
could be shortened to
d = document
d[rk][XU](d[Ou]("script")).src="URL"
this specific approach takes every key on the document, turns it into a large number by multiplying each codepoint with an index and then summing and modding by 3456 (54 * 64), which is the maximum number of items representable with a two-byte javascript identifier:
/[a-z_$][0-9a-z_$]/i
this yields decent results. in an empty chrome page, there were only 3 collisions. the idea is that the upper bound can be lowered as necessary, until the collisions are acceptable.
This approach is definitely viable. No doubt about it.
And as a general purpose hashing technique, this is pretty good: Fast, simple and quite compact already. Of course the implementation can be shortened.
Better in which way ? It depends on the use case. For a js1k prod, I would go for something more specific and therefore more compact.