Skip to content

Commit

Permalink
perf(core): adding micro benchmark for host bindings (#35705)
Browse files Browse the repository at this point in the history
This commit adds micro benchmark for host bindings, so that we can assess the impact of changes related to host bindings (for example PR #35568).

PR Close #35705
  • Loading branch information
AndrewKushnir authored and atscott committed Feb 27, 2020
1 parent d06b6de commit 4ec7cd1
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/core/test/render3/perf/BUILD.bazel
Expand Up @@ -216,3 +216,16 @@ ng_benchmark(
name = "duplicate_map_based_style_and_class_bindings",
bundle = ":duplicate_map_based_style_and_class_bindings_lib",
)

ng_rollup_bundle(
name = "host_binding_lib",
entry_point = ":host_binding/index.ts",
deps = [
":perf_lib",
],
)

ng_benchmark(
name = "host_binding",
bundle = ":host_binding",
)
84 changes: 84 additions & 0 deletions packages/core/test/render3/perf/host_binding/index.ts
@@ -0,0 +1,84 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {ɵɵdefineDirective, ɵɵelement, ɵɵhostProperty, ɵɵlistener} from '../../../../src/render3/index';
import {DirectiveDefList, RenderFlags} from '../../../../src/render3/interfaces/definition';
import {TAttributes} from '../../../../src/render3/interfaces/node';
import {createBenchmark} from '../micro_bench';
import {setupTestHarness} from '../setup';

`
@Directive({
selector: '[hostBindingDir]'
})
export class HostBindingDir {
exp = 'string-exp';
@HostBinding('data-a')
a: string = 'exp';
@HostListener('click')
onClick(event: any): void {}
}
`;
class HostBindingDir {
static ɵfac() { return new HostBindingDir(); }
static ɵdir = ɵɵdefineDirective({
type: HostBindingDir,
selectors: [['', 'hostBindingDir', '']],
hostVars: 2,
hostBindings: function(rf: RenderFlags, ctx: any) {
if (rf & 1) {
ɵɵlistener('click', function() { return ctx.onClick(); });
}
if (rf & 2) {
ɵɵhostProperty('data-a', ctx.exp);
}
}
});

exp = 'string-exp';
onClick() {}
}

`
<div hostBindingDir></div>
`;
function componentTemplateFn(rf: RenderFlags, ctx: any) {
if (rf & 1) {
ɵɵelement(0, 'div', 0);
}
}

const context: any = {};
const consts: TAttributes[] = [['hostBindingDir', '']];
const directives: DirectiveDefList = [HostBindingDir.ɵdir];
const harness = setupTestHarness(componentTemplateFn, 1, 0, 1000, context, consts, directives);

// Benchmark host bindings execution in *creation* mode
const createModeBenchmark = createBenchmark('host bindings');
const createModeProfile = createModeBenchmark('`create` mode');

console.profile(createModeBenchmark.name + ':' + createModeProfile.name);
while (createModeProfile()) {
harness.createEmbeddedLView();
}
console.profileEnd();

createModeBenchmark.report();

// Benchmark host bindings execution in *update* mode
const updateModeBenchmark = createBenchmark('host bindings');
const updateModeProfile = updateModeBenchmark('`update` mode');

console.profile(updateModeBenchmark.name + ':' + updateModeProfile.name);
while (updateModeProfile()) {
harness.detectChanges();
}
console.profileEnd();

updateModeBenchmark.report();

0 comments on commit 4ec7cd1

Please sign in to comment.