|
7 | 7 | * @flow
|
8 | 8 | */
|
9 | 9 |
|
| 10 | +import LRU from 'lru-cache'; |
10 | 11 | import {
|
11 | 12 | isElement,
|
12 | 13 | typeOf,
|
@@ -49,9 +50,16 @@ import {localStorageGetItem, localStorageSetItem} from './storage';
|
49 | 50 | import {meta} from './hydration';
|
50 | 51 |
|
51 | 52 | import type {ComponentFilter, ElementType} from './types';
|
| 53 | +import type {LRUCache} from 'react-devtools-shared/src/types'; |
52 | 54 |
|
53 | 55 | const cachedDisplayNames: WeakMap<Function, string> = new WeakMap();
|
54 | 56 |
|
| 57 | +// On large trees, encoding takes significant time. |
| 58 | +// Try to reuse the already encoded strings. |
| 59 | +const encodedStringCache: LRUCache<string, Array<number>> = new LRU({ |
| 60 | + max: 1000, |
| 61 | +}); |
| 62 | + |
55 | 63 | export function alphaSortKeys(
|
56 | 64 | a: string | number | Symbol,
|
57 | 65 | b: string | number | Symbol,
|
@@ -140,21 +148,27 @@ function surrogatePairToCodePoint(
|
140 | 148 | // Credit for this encoding approach goes to Tim Down:
|
141 | 149 | // https://stackoverflow.com/questions/4877326/how-can-i-tell-if-a-string-contains-multibyte-characters-in-javascript
|
142 | 150 | export function utfEncodeString(string: string): Array<number> {
|
143 |
| - const codePoints = []; |
| 151 | + const cached = encodedStringCache.get(string); |
| 152 | + if (cached !== undefined) { |
| 153 | + return cached; |
| 154 | + } |
| 155 | + |
| 156 | + const encoded = []; |
144 | 157 | let i = 0;
|
145 | 158 | let charCode;
|
146 | 159 | while (i < string.length) {
|
147 | 160 | charCode = string.charCodeAt(i);
|
148 | 161 | if ((charCode & 0xf800) === 0xd800) {
|
149 |
| - codePoints.push( |
150 |
| - surrogatePairToCodePoint(charCode, string.charCodeAt(++i)), |
151 |
| - ); |
| 162 | + encoded.push(surrogatePairToCodePoint(charCode, string.charCodeAt(++i))); |
152 | 163 | } else {
|
153 |
| - codePoints.push(charCode); |
| 164 | + encoded.push(charCode); |
154 | 165 | }
|
155 | 166 | ++i;
|
156 | 167 | }
|
157 |
| - return codePoints; |
| 168 | + |
| 169 | + encodedStringCache.set(string, encoded); |
| 170 | + |
| 171 | + return encoded; |
158 | 172 | }
|
159 | 173 |
|
160 | 174 | export function printOperationsArray(operations: Array<number>) {
|
|
0 commit comments