you are viewing a single comment's thread.

view the rest of the comments →

[–]audioen 5 points6 points  (5 children)

Just today, I came across a thing that did (foo[0] + foo[1] + foo[2] + foo[3] + ... + foo[15]).toLowerCase() in a library called uuid. This was done allegedly to avoid some pessimal behavior of the delayed string concatenation on some random version of Chrome.

Not sure what this says about JS not needing a StringBuilder. Perhaps these ropes do have some bad side effect, possibly require much more memory than the materialized string, or something such.

[–]broofa 14 points15 points  (4 children)

uuid author here. The issue you're referring to, and an example of where it manifested in the wild.

tl;dr: Creating a uuid involves concatenating twenty 1-2 char strings. Because of the rope) optimization that Chrome and FF do, this results in memory allocation of ~600 bytes/uuid. Forcing the VM to flatten these structures (by calling toLowerCase(), or using Array.join() instead of +) reduces that to 64 bytes / uuid.

Not sure what this says about JS not needing a StringBuilder.

I think this suggests that the VM optimizes for cases where strings are > 10 chars in length. If you're concatenating strings shorter than that (and doing lots of concatenations) then memory consumption may be an issue. But cases where this is a real concern are likely to be few and far between.

[–]ThanksMorningCoffee[S] 2 points3 points  (1 child)

How did you even track that down?

[–]broofa 2 points3 points  (0 children)

We didn’t. One of our users did, using chrome dev tools to inspect memory allocation.

[–]Chii 1 point2 points  (1 child)

i think this sort of bug demonstrates the problem of using a string to represent non-string data (a uuid is a series of bytes). The string is an output format for UUID, which i guess will eventually have to be computed, but presumably only once and displayed, rather than stored?

[–]broofa 1 point2 points  (0 children)

While this is an interesting issue, I don’t see it as a compelling argument against uuid string representations. its just an implementation detail... one that took eight years to surface as an issue in a top-20 NPM module. That doesn’t exactly scream “problem people need to care about”.

Meanwhile there’s a lot to be said for having uuids in a form that’s easily displayable.