Skip to content

Commit

Permalink
Cache the result in DEV
Browse files Browse the repository at this point in the history
In DEV it's somewhat likely that we'll see many logs that add component
stacks. This could be slow so we cache the results of previous components.
  • Loading branch information
sebmarkbage committed Apr 10, 2020
1 parent bde12f6 commit 97fac34
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions packages/shared/ReactComponentStackFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ export function describeBuiltInComponentFrame(
}

let reentry = false;
let componentFrameCache;
if (__DEV__) {
const PossiblyWeakMap = typeof WeakMap === 'function' ? WeakMap : Map;
componentFrameCache = new PossiblyWeakMap();
}

export function describeNativeComponentFrame(
fn: Function,
Expand All @@ -63,6 +68,13 @@ export function describeNativeComponentFrame(
return '';
}

if (__DEV__) {
const frame = componentFrameCache.get(fn);
if (frame !== undefined) {
return frame;
}
}

const control = Error();

reentry = true;
Expand Down Expand Up @@ -119,7 +131,11 @@ export function describeNativeComponentFrame(
if (sampleLines[s] !== controlLines[c]) {
// Return the line we found.
// V8 adds a "new" prefix for native classes. Let's remove it to make it prettier.
return '\n' + sampleLines[s - 1].replace(' at new ', ' at ');
const frame = '\n' + sampleLines[s - 1].replace(' at new ', ' at ');
if (__DEV__) {
componentFrameCache.set(fn, frame);
}
return frame;
}
}
}
Expand All @@ -132,7 +148,11 @@ export function describeNativeComponentFrame(
}
// Fallback to just using the name if we couldn't make it throw.
const name = fn ? fn.displayName || fn.name : '';
return name ? describeBuiltInComponentFrame(name) : '';
const syntheticFrame = name ? describeBuiltInComponentFrame(name) : '';
if (__DEV__) {
componentFrameCache.set(fn, syntheticFrame);
}
return syntheticFrame;
}

const BEFORE_SLASH_RE = /^(.*)[\\\/]/;
Expand Down

0 comments on commit 97fac34

Please sign in to comment.