Skip to content

Commit f33af13

Browse files
authored
Removed OptimizelyConfig creation from SDK initialization to reduce intialization time (#704)
## Summary `OptimizelyConfig` object is created during initialization of `Optimizely` Instance. This almost doubles the initialization time for the SDK. This PR moves the creation of `OptimizelyConfig` object to `getOptimizelyConfig`. The `OptimizelyConfig` object will be created and cached when the `getOptimizelyConfig` API is called for the first time after every data file update. ## Test plan - Manually tested thoroughly - Updated unit tests - All FSC test pass
1 parent bfa49ae commit f33af13

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

packages/optimizely-sdk/lib/core/project_config/project_config_manager.tests.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,6 @@ describe('lib/core/project_config/project_config_manager', function() {
425425
datafile: testData.getTestProjectConfig(),
426426
sdkKey: '12345',
427427
});
428-
// creating optimizely config once project config manager for the first time
429-
sinon.assert.calledOnce(optimizelyConfig.createOptimizelyConfig);
430428
// validate it should return the existing optimizely config
431429
manager.getOptimizelyConfig();
432430
sinon.assert.calledOnce(optimizelyConfig.createOptimizelyConfig);
@@ -437,6 +435,7 @@ describe('lib/core/project_config/project_config_manager', function() {
437435
newDatafile.revision = '36';
438436
fakeDatafileManager.get.returns(newDatafile);
439437
updateListener({ datafile: newDatafile });
438+
manager.getOptimizelyConfig();
440439
// verify the optimizely config is updated
441440
sinon.assert.calledTwice(optimizelyConfig.createOptimizelyConfig);
442441
});

packages/optimizely-sdk/lib/core/project_config/project_config_manager.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ export class ProjectConfigManager {
214214
const oldRevision = this.configObj ? this.configObj.revision : 'null';
215215
if (configObj && oldRevision !== configObj.revision) {
216216
this.configObj = configObj;
217-
this.optimizelyConfigObj = createOptimizelyConfig(this.configObj, toDatafile(this.configObj));
217+
this.optimizelyConfigObj = null;
218218
this.updateListeners.forEach((listener) => listener(configObj));
219219
}
220220
}
@@ -236,6 +236,9 @@ export class ProjectConfigManager {
236236
* @return {OptimizelyConfig|null}
237237
*/
238238
getOptimizelyConfig(): OptimizelyConfig | null {
239+
if (!this.optimizelyConfigObj && this.configObj) {
240+
this.optimizelyConfigObj = createOptimizelyConfig(this.configObj, toDatafile(this.configObj));
241+
}
239242
return this.optimizelyConfigObj;
240243
}
241244

0 commit comments

Comments
 (0)