Skip to content

Commit c11ca94

Browse files
committed
perf(ElementInjector): add a benchmark measuring the instantiation of element injectors without using reflection
1 parent e3b7724 commit c11ca94

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
library element_injector_benchmark;
22

33
import './instantiate_benchmark.dart' as ib;
4+
import './instantiate_benchmark_codegen.dart' as ibc;
5+
import './instantiate_directive_benchmark.dart' as idb;
46
import 'dart:js' as js;
57

68
main () {
79
js.context['benchmarkSteps'].add(new js.JsObject.jsify({
810
"name": "ElementInjector.instantiate + instantiateDirectives",
911
"fn": new js.JsFunction.withThis((_) => ib.run())
1012
}));
13+
14+
js.context['benchmarkSteps'].add(new js.JsObject.jsify({
15+
"name": "ElementInjector.instantiateDirectives",
16+
"fn": new js.JsFunction.withThis((_) => idb.run())
17+
}));
18+
19+
js.context['benchmarkSteps'].add(new js.JsObject.jsify({
20+
"name": "ElementInjector.instantiate + instantiateDirectives (codegen)",
21+
"fn": new js.JsFunction.withThis((_) => ibc.run())
22+
}));
1123
}

modules/benchmarks/src/element_injector/benchmark.es5

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ System.import('benchmarks/element_injector/instantiate_benchmark').then(function
44

55
System.import('benchmarks/element_injector/instantiate_directive_benchmark').then(function (bm) {
66
window.benchmarkSteps.push({name: 'ElementInjector.instantiateDirectives', fn: bm.run});
7+
}, console.log.bind(console));
8+
9+
System.import('benchmarks/element_injector/instantiate_benchmark_codegen').then(function (bm) {
10+
window.benchmarkSteps.push({name: 'ElementInjector.instantiate + instantiateDirectives (codegen)', fn: bm.run});
711
}, console.log.bind(console));

modules/benchmarks/src/element_injector/instantiate_benchmark.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import {Injector} from 'di/di';
22
import {ProtoElementInjector} from 'core/compiler/element_injector';
33

4+
var ITERATIONS = 20000;
45
var count = 0;
56

67
export function run () {
78
var appInjector = new Injector([]);
89

910
var bindings = [A, B, C];
1011
var proto = new ProtoElementInjector(null, bindings, []);
11-
for (var i = 0; i < 20000; ++i) {
12+
for (var i = 0; i < ITERATIONS; ++i) {
1213
var ei = proto.instantiate({view:null});
1314
ei.instantiateDirectives(appInjector);
1415
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {Binding, Dependency, Key, Injector} from 'di/di';
2+
import {ProtoElementInjector} from 'core/compiler/element_injector';
3+
4+
var ITERATIONS = 20000;
5+
var count = 0;
6+
7+
export function run () {
8+
var appInjector = new Injector([]);
9+
10+
var bindings = [
11+
new Binding(Key.get(A), () => new A(), [], false),
12+
new Binding(Key.get(B), () => new B(), [], false),
13+
new Binding(Key.get(C), (a,b) => new C(a,b), [
14+
new Dependency(Key.get(A), false, false, []),
15+
new Dependency(Key.get(B), false, false, [])
16+
], false)];
17+
18+
19+
var proto = new ProtoElementInjector(null, bindings, []);
20+
for (var i = 0; i < ITERATIONS; ++i) {
21+
var ei = proto.instantiate({view:null});
22+
ei.instantiateDirectives(appInjector);
23+
}
24+
}
25+
26+
class A {
27+
constructor() {
28+
count++;
29+
}
30+
}
31+
32+
class B {
33+
constructor() {
34+
count++;
35+
}
36+
}
37+
38+
class C {
39+
constructor(a:A, b:B) {
40+
count++;
41+
}
42+
}

modules/benchmarks/src/element_injector/instantiate_directive_benchmark.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Injector} from 'di/di';
22
import {ProtoElementInjector} from 'core/compiler/element_injector';
33

4+
var ITERATIONS = 20000;
45
var count = 0;
56

67
export function run () {
@@ -10,7 +11,7 @@ export function run () {
1011
var proto = new ProtoElementInjector(null, bindings, []);
1112
var ei = proto.instantiate({view:null});
1213

13-
for (var i = 0; i < 20000; ++i) {
14+
for (var i = 0; i < ITERATIONS; ++i) {
1415
ei.clearDirectives();
1516
ei.instantiateDirectives(appInjector);
1617
}

0 commit comments

Comments
 (0)