6 ms·
Call Symbol.for(string). This accesses a set of existing symbols called the symbol registry. Unlike the unique symbols defined by Symbol(), symbols in the symbo
by agd 11y ago
Call Symbol.for(string). This accesses a set of existing symbols called the symbol registry. Unlike the unique symbols defined by Symbol(), symbols in the symbol registry are shared. If you call Symbol.for("cat") thirty times, it will return the same symbol each time. The registry is useful when multiple web pages, or multiple modules within the same web page, need to share a symbol.
Have they added inbuilt support for globals here? People could easily add their own global symbols, why does this need to be built in?
- carussell 11y agoCould you reword the question? I don't understand what you're asking.
- agd 11y agoI guess I don't understand why the global symbol registry exists. If someone wants a global symbol map, they can create their own. What else does this add over a user implementation, and what are the use cases?
- pavlov 11y agoMaking the registry global allows compilers to optimize away those Symbol.for() calls, since they know that each call to Symbol.for("cat") is going to resolve to the same symbol instance.
- agd 11y agoBut if people are concerned about performance, they can just pre-allocate/memoize the symbols. It doesn't seem like the registry adds any new functionality, and it might encourage use of globals. Surely that's a bad thing?