Skip to content

Commit 0736f5d

Browse files
fromcelticparkfacebook-github-bot
authored andcommitted
Make Set and Map initialization lazy
Reviewed By: javache Differential Revision: D5461810 fbshipit-source-id: 6f22528bac4dd6e073e98a093e02d84114d0706a
1 parent 98258b4 commit 0736f5d

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

Libraries/Core/InitializeCore.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,13 @@ if (!global.__fbDisableExceptionsManager) {
128128
}
129129

130130
// Set up collections
131-
// We can't make these lazy because `Map` checks for `global.Map` (which wouldc
132-
// not exist if it were lazily defined).
133-
defineProperty(global, 'Map', () => require('Map'), true);
134-
defineProperty(global, 'Set', () => require('Set'), true);
131+
const _shouldPolyfillCollection = require('_shouldPolyfillES6Collection');
132+
if (_shouldPolyfillCollection('Map')) {
133+
defineProperty(global, 'Map', () => require('Map'));
134+
}
135+
if (_shouldPolyfillCollection('Set')) {
136+
defineProperty(global, 'Set', () => require('Set'));
137+
}
135138

136139
// Set up Promise
137140
// The native Promise implementation throws the following error:

Libraries/vendor/core/_shouldPolyfillES6Collection.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* Checks whether a collection name (e.g. "Map" or "Set") has a native polyfill
1717
* that is safe to be used.
1818
*/
19-
function shouldPolyfillES6Collection(collectionName: string): boolean {
19+
function _shouldActuallyPolyfillES6Collection(collectionName: string): boolean {
2020
var Collection = global[collectionName];
2121
if (Collection == null) {
2222
return true;
@@ -43,4 +43,23 @@ function shouldPolyfillES6Collection(collectionName: string): boolean {
4343
typeof proto.forEach !== 'function';
4444
}
4545

46-
module.exports = shouldPolyfillES6Collection;
46+
const cache: { [name: string]: bool } = {};
47+
48+
/**
49+
* Checks whether a collection name (e.g. "Map" or "Set") has a native polyfill
50+
* that is safe to be used and caches this result.
51+
* Make sure to make a first call to this function before a corresponding
52+
* property on global was overriden in any way.
53+
*/
54+
function _shouldPolyfillES6Collection(collectionName: string) {
55+
let result = cache[collectionName];
56+
if (result !== undefined) {
57+
return result;
58+
}
59+
60+
result = _shouldActuallyPolyfillES6Collection(collectionName);
61+
cache[collectionName] = result;
62+
return result;
63+
}
64+
65+
module.exports = _shouldPolyfillES6Collection;

0 commit comments

Comments
 (0)