Skip to content

Commit

Permalink
perf: optimizing detection of global overrides help-me-mom#1452
Browse files Browse the repository at this point in the history
  • Loading branch information
satanTime committed Mar 26, 2022
1 parent df4a781 commit b7c0b77
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ jobs:
- run:
name: MockRender
command: KARMA_SUITE=tests-performance/mock-render.spec.ts npm run test
- run:
name: Large Modules
command: KARMA_SUITE=tests-performance/ng-mocks.spec.ts npm run test
'Angular 5 ES5':
docker:
- image: satantime/puppeteer-node:6.17.1
Expand Down
21 changes: 17 additions & 4 deletions libs/ng-mocks/src/lib/common/core.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,29 @@ export const mapKeys = <T>(set: Map<T, any>): T[] => {
return result;
};

export const mapValues = <T>(set: { forEach(a1: (value: T) => void): void }): T[] => {
export const mapValues = <T>(set: { forEach(a1: (value: T) => void): void }, destination?: Set<T>): T[] => {
const result: T[] = [];
set.forEach((value: T) => result.push(value));
if (destination) {
set.forEach((value: T) => {
destination.add(value);
});
} else {
set.forEach((value: T) => {
result.push(value);
});
}

return result;
};

export const mapEntries = <K, T>(set: Map<K, T>): Array<[K, T]> => {
export const mapEntries = <K, T>(set: Map<K, T>, destination?: Map<K, T>): Array<[K, T]> => {
const result: Array<[K, T]> = [];
set.forEach((value: T, key: K) => result.push([key, value]));

if (destination) {
set.forEach((value: T, key: K) => destination.set(key, value));
} else {
set.forEach((value: T, key: K) => result.push([key, value]));
}

return result;
};
Expand Down
25 changes: 19 additions & 6 deletions libs/ng-mocks/src/lib/common/ng-mocks-global-overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import mockHelperFasterInstall from '../mock-helper/mock-helper.faster-install';
import { MockProvider } from '../mock-provider/mock-provider';

import coreDefineProperty from './core.define-property';
import { flatten, mapEntries } from './core.helpers';
import { flatten, mapEntries, mapValues } from './core.helpers';
import coreReflectMeta from './core.reflect.meta';
import coreReflectModuleResolve from './core.reflect.module-resolve';
import coreReflectProvidedIn from './core.reflect.provided-in';
Expand Down Expand Up @@ -46,13 +46,13 @@ const applyOverrides = (overrides: Map<AnyType<any>, [MetadataOverride<any>, Met

// Thanks Ivy and its TestBed.override - it does not clean up leftovers.
const applyNgMocksOverrides = (testBed: TestBedStatic & { ngMocksOverrides?: Map<any, any> }): void => {
if (testBed.ngMocksOverrides) {
if (testBed.ngMocksOverrides?.size) {
ngMocks.flushTestBed();
for (const [def, original] of mapEntries(testBed.ngMocksOverrides)) {
applyOverride(def, original);
}
testBed.ngMocksOverrides = undefined;
}
testBed.ngMocksOverrides = undefined;
};

const initTestBed = () => {
Expand Down Expand Up @@ -86,11 +86,24 @@ const generateTouches = (
generateTouches(def, touches);
def = def.ngModule;
}
if (touches.has(def)) {
continue;
}
touches.add(def);
const meta = coreReflectMeta(def);
if (meta) {
generateTouches(meta, touches);
if (typeof def !== 'function') {
continue;
}

if (!def.hasOwnProperty('__ngMocksTouches')) {
const local = new Set<any>();
const meta = coreReflectMeta(def);
if (meta) {
generateTouches(meta, local);
}
coreDefineProperty(def, '__ngMocksTouches', local, false);
}

mapValues(def.__ngMocksTouches, touches);
}
}
};
Expand Down
21 changes: 10 additions & 11 deletions libs/ng-mocks/src/lib/mock-builder/mock-builder.performance.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { TestBed, TestModuleMetadata } from '@angular/core/testing';

import { mapEntries, mapValues } from '../common/core.helpers';
import ngMocksUniverse from '../common/ng-mocks-universe';

import { MockBuilderPromise } from './mock-builder.promise';
import addEntitiesToMap from './performance/add-entities-to-map';
import addValuesToSet from './performance/add-values-to-set';
import areEqualConfigParams from './performance/are-equal-config-params';
import areEqualMaps from './performance/are-equal-maps';
import areEqualProviders from './performance/are-equal-providers';
Expand Down Expand Up @@ -65,16 +64,16 @@ export class MockBuilderPerformance extends MockBuilderPromise {
private cloneConfig() {
const config = getEmptyConfig();

addValuesToSet(this.beforeCC, config.beforeCC);
addValuesToSet(this.excludeDef, config.excludeDef);
addValuesToSet(this.keepDef, config.keepDef);
addValuesToSet(this.mockDef, config.mockDef);
addValuesToSet(this.replaceDef, config.replaceDef);
mapValues(this.beforeCC, config.beforeCC);
mapValues(this.excludeDef, config.excludeDef);
mapValues(this.keepDef, config.keepDef);
mapValues(this.mockDef, config.mockDef);
mapValues(this.replaceDef, config.replaceDef);

addEntitiesToMap(this.configDef, config.configDef);
addEntitiesToMap(this.defProviders, config.defProviders);
addEntitiesToMap(this.defValue, config.defValue);
addEntitiesToMap(this.providerDef, config.providerDef);
mapEntries(this.configDef, config.configDef);
mapEntries(this.defProviders, config.defProviders);
mapEntries(this.defValue, config.defValue);
mapEntries(this.providerDef, config.providerDef);

return config;
}
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit b7c0b77

Please sign in to comment.