From a89e9323fccf17ab3e5c3de9fcf22ec409baeef6 Mon Sep 17 00:00:00 2001 From: Kevin Gozali Date: Tue, 18 Jun 2019 23:07:20 -0700 Subject: [PATCH] Cache Platform constants in JS Summary: For better perf with TurboModule, cache the return value of NativePlatformConstants*.getConstants() in JS so that we avoid going back into native (from JS) for each call. This specific method is called very frequently throughout RN codebase. Reviewed By: mdvacca Differential Revision: D15893289 fbshipit-source-id: ce8016ed7d3efb420df93e27dbfa77d7d4f06cf8 --- Libraries/Utilities/Platform.android.js | 12 ++++++++---- Libraries/Utilities/Platform.ios.js | 14 +++++++++----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Libraries/Utilities/Platform.android.js b/Libraries/Utilities/Platform.android.js index d7e672b33b8..e228adc98d2 100644 --- a/Libraries/Utilities/Platform.android.js +++ b/Libraries/Utilities/Platform.android.js @@ -18,21 +18,25 @@ export type PlatformSelectSpec = { }; const Platform = { + __constants: null, OS: 'android', get Version() { - return NativePlatformConstantsAndroid.getConstants().Version; + return this.constants.Version; }, get constants() { - return NativePlatformConstantsAndroid.getConstants(); + if (this.__constants == null) { + this.__constants = NativePlatformConstantsAndroid.getConstants(); + } + return this.__constants; }, get isTesting(): boolean { if (__DEV__) { - return NativePlatformConstantsAndroid.getConstants().isTesting; + return this.constants.isTesting; } return false; }, get isTV(): boolean { - return NativePlatformConstantsAndroid.getConstants().uiMode === 'tv'; + return this.constants.uiMode === 'tv'; }, select: (spec: PlatformSelectSpec): A | D => 'android' in spec ? spec.android : spec.default, diff --git a/Libraries/Utilities/Platform.ios.js b/Libraries/Utilities/Platform.ios.js index f558755c6f0..0bdb6d35a62 100644 --- a/Libraries/Utilities/Platform.ios.js +++ b/Libraries/Utilities/Platform.ios.js @@ -18,15 +18,19 @@ export type PlatformSelectSpec = { }; const Platform = { + __constants: null, OS: 'ios', get Version() { - return NativePlatformConstantsIOS.getConstants().osVersion; + return this.constants.osVersion; }, get constants() { - return NativePlatformConstantsIOS.getConstants(); + if (this.__constants == null) { + this.__constants = NativePlatformConstantsIOS.getConstants(); + } + return this.__constants; }, get isPad() { - return NativePlatformConstantsIOS.getConstants().interfaceIdiom === 'pad'; + return this.constants.interfaceIdiom === 'pad'; }, /** * Deprecated, use `isTV` instead. @@ -35,11 +39,11 @@ const Platform = { return Platform.isTV; }, get isTV() { - return NativePlatformConstantsIOS.getConstants().interfaceIdiom === 'tv'; + return this.constants.interfaceIdiom === 'tv'; }, get isTesting(): boolean { if (__DEV__) { - return NativePlatformConstantsIOS.getConstants().isTesting; + return this.constants.isTesting; } return false; },