Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit cb9e872

Browse files
committed
feat(core): scoped zone
1 parent 6b70d2f commit cb9e872

File tree

7 files changed

+68
-1
lines changed

7 files changed

+68
-1
lines changed

lib/browser/browser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import {findEventTasks} from '../common/events';
1414
import {patchTimer} from '../common/timers';
15-
import {bindArguments, patchClass, patchMacroTask, patchMethod, patchOnProperties, patchPrototype, scheduleMacroTaskWithCurrentZone, ZONE_SYMBOL_ADD_EVENT_LISTENER, ZONE_SYMBOL_REMOVE_EVENT_LISTENER, zoneSymbol} from '../common/utils';
15+
import {attachOriginToPatched, bindArguments, patchClass, patchMacroTask, patchMethod, patchOnProperties, patchPrototype, scheduleMacroTaskWithCurrentZone, ZONE_SYMBOL_ADD_EVENT_LISTENER, ZONE_SYMBOL_REMOVE_EVENT_LISTENER, zoneSymbol} from '../common/utils';
1616

1717
import {propertyPatch} from './define-property';
1818
import {eventTargetPatch, patchEvent} from './event-target';
@@ -23,6 +23,7 @@ Zone.__load_patch('util', (global: any, Zone: ZoneType, api: _ZonePrivate) => {
2323
api.patchOnProperties = patchOnProperties;
2424
api.patchMethod = patchMethod;
2525
api.bindArguments = bindArguments;
26+
api.attachOriginToPatched = attachOriginToPatched;
2627
});
2728

2829
Zone.__load_patch('timers', (global: any) => {

lib/browser/webapis-user-media.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Zone.__load_patch('getUserMedia', (global: any, Zone: any, api: _ZonePrivate) =>
1515
}
1616
let navigator = global['navigator'];
1717
if (navigator && navigator.getUserMedia) {
18+
const native = navigator.getUserMedia;
1819
navigator.getUserMedia = wrapFunctionArgs(navigator.getUserMedia);
20+
api.attachOriginToPatched(navigator, 'getUserMedia', native);
1921
}
2022
});

lib/browser/websocket.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export function apply(api: _ZonePrivate, _global: any) {
5555
};
5656

5757
const globalWebSocket = _global['WebSocket'];
58+
api.attachOriginToPatched(_global, 'WebSocket', WS);
5859
for (const prop in WS) {
5960
globalWebSocket[prop] = WS[prop];
6061
}

lib/common/promise.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
445445
}
446446

447447
global['Promise'] = ZoneAwarePromise;
448+
api.attachOriginToPatched(global, 'Promise', NativePromise);
448449

449450
const symbolThenPatched = __symbol__('thenPatched');
450451

lib/zone.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ interface _ZonePrivate {
329329
patchFn: (delegate: Function, delegateName: string, name: string) =>
330330
(self: any, args: any[]) => any) => Function | null;
331331
bindArguments: (args: any[], source: string) => any[];
332+
attachOriginToPatched: (patchedTarget: any, prop: string, original: any) => void;
332333
}
333334

334335
/** @internal */
@@ -1403,6 +1404,7 @@ const Zone: ZoneType = (function(global: any) {
14031404
nativeMicroTaskQueuePromise = NativePromise.resolve(0);
14041405
}
14051406
},
1407+
attachOriginToPatched: (patchedTarget: any, prop: string, original: any) => noop
14061408
};
14071409
let _currentZoneFrame: _ZoneFrame = {parent: null, zone: new Zone(null, null)};
14081410
let _currentTask: Task|null = null;

test/browser/performance.spec.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
describe('Performance', () => {
10+
function mark(name: string) {
11+
performance && performance['mark'] && performance['mark'](name);
12+
}
13+
function performanceMeasure(name: string, label: string) {
14+
performance && performance['measure'] && performance['measure'](name, label);
15+
}
16+
function getEntriesByName(name: string) {
17+
return performance && performance['getEntriesByName'] &&
18+
performance['getEntriesByName'](name).filter(m => m.entryType === 'measure');
19+
}
20+
describe('Lazy mode', () => {
21+
const rootZone = Zone.root;
22+
const noop = function() {};
23+
const normal = function() {
24+
for (let i = 0; i < 10000; i++) {
25+
let j = i * i;
26+
}
27+
};
28+
const times = 100000;
29+
describe('root zone performance benchmark', () => {
30+
it('noop function', () => {
31+
const normal = 'rootZone normal noop';
32+
mark(normal);
33+
for (let i = 0; i < times; i++) {
34+
rootZone.run(noop);
35+
}
36+
performanceMeasure(normal, normal);
37+
const normalEntries = getEntriesByName(normal);
38+
const normalDuration = normalEntries[0].duration;
39+
const normalAverage = normalDuration / times;
40+
console.log(`${normal} ${times} times cost: ${normalDuration}, ${normalAverage} average`);
41+
const lazy = 'rootZone lazy noop';
42+
mark(lazy);
43+
(Zone as any)._mode = 'lazy';
44+
for (let i = 0; i < 100000; i++) {
45+
rootZone.run(noop);
46+
}
47+
performanceMeasure(lazy, lazy);
48+
const lazyEntries = getEntriesByName(lazy);
49+
const lazyDuration = lazyEntries[0].duration;
50+
const lazyAverage = lazyDuration / times;
51+
console.log(`${lazy} ${times} times cost: ${lazyDuration}, ${lazyAverage}`);
52+
(Zone as any)._mode = 'normal';
53+
const diff = lazyDuration - normalDuration;
54+
const diffAverage = diff / times;
55+
console.log(`diff running ${times} times is: ${diff}, average is ${diffAverage}`);
56+
});
57+
});
58+
});
59+
});

test/browser_entry_point.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ import './browser/Worker.spec';
2727
import './mocha-patch.spec';
2828
import './jasmine-patch.spec';
2929
import './extra/cordova.spec';
30+
import './browser/performance.spec';

0 commit comments

Comments
 (0)