python-visualization / branca

This library is a spinoff from folium, that would host the non-map-specific features.
https://python-visualization.github.io/branca/
MIT License
111 stars 63 forks source link

Speed up UUID creation for `Element._id` #101

Closed bwest2397 closed 2 years ago

bwest2397 commented 2 years ago

uuid.uuid4().hex, used in Element.__init__ to define self._id, is considerably slower than binascii.hexlify(os.urandom(16)).decode(). uuid4 uses os.urandom(16) under the hood when creating a cryptographically-secure UUID, so creating UUIDs for Element-class objects using hexlify leads to a dramatic speed-up (I found about ~60%), which has implications for subclasses of Element. No new requirements are needed as binascii is already in the Python standard library, so this is essentially a free, pretty big speed bump (especially when considering that some Element subclasses in folium themselves create child instances of Element when instantiated).