UNPKG

1.6 kBTypeScriptView Raw
1/**
2 Strongly hint runtimes to intern the provided string.
3
4 When do I need to use this function?
5
6 For the most part, never. Pre-mature optimization is bad, and often the
7 runtime does exactly what you need it to, and more often the trade-off isn't
8 worth it.
9
10 Why?
11
12 Runtimes store strings in at least 2 different representations:
13 Ropes and Symbols (interned strings). The Rope provides a memory efficient
14 data-structure for strings created from concatenation or some other string
15 manipulation like splitting.
16
17 Unfortunately checking equality of different ropes can be quite costly as
18 runtimes must resort to clever string comparison algorithms. These
19 algorithms typically cost in proportion to the length of the string.
20 Luckily, this is where the Symbols (interned strings) shine. As Symbols are
21 unique by their string content, equality checks can be done by pointer
22 comparison.
23
24 How do I know if my string is a rope or symbol?
25
26 Typically (warning general sweeping statement, but truthy in runtimes at
27 present) static strings created as part of the JS source are interned.
28 Strings often used for comparisons can be interned at runtime if some
29 criteria are met. One of these criteria can be the size of the entire rope.
30 For example, in chrome 38 a rope longer then 12 characters will not
31 intern, nor will segments of that rope.
32
33 Some numbers: http://jsperf.com/eval-vs-keys/8
34
35 Known Trick™
36
37 @private
38 @return {String} interned version of the provided string
39*/
40export default function intern(str: string): string;
41//# sourceMappingURL=intern.d.ts.map
\No newline at end of file