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

Commit f3de5b6

Browse files
kseamonIgorMinar
authored andcommitted
perf(a): do not link when href or name exists in template
Change the a directive to link and hookup a click event only when there is no href or name in the template element. In a large Google app, this results in about 800 fewer registrations, saving a small but measurable amount of time and memory. Closes #5362
1 parent fcd2a81 commit f3de5b6

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

src/ng/directive/a.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ var htmlAnchorDirective = valueFn({
3232
element.append(document.createComment('IE fix'));
3333
}
3434

35-
return function(scope, element) {
36-
element.on('click', function(event){
37-
// if we have no href url, then don't navigate anywhere.
38-
if (!element.attr('href')) {
39-
event.preventDefault();
40-
}
41-
});
42-
};
35+
if (!attr.href && !attr.name) {
36+
return function(scope, element) {
37+
element.on('click', function(event){
38+
// if we have no href url, then don't navigate anywhere.
39+
if (!element.attr('href')) {
40+
event.preventDefault();
41+
}
42+
});
43+
};
44+
}
4345
}
4446
});

test/ng/directive/aSpec.js

+26
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,30 @@ describe('a', function() {
5858

5959
expect(element.text()).toBe('hello@you');
6060
});
61+
62+
63+
it('should not link and hookup an event if href is present at compile', function() {
64+
var jq = jQuery || jqLite;
65+
element = jq('<a href="//a.com">hello@you</a>');
66+
var linker = $compile(element);
67+
68+
spyOn(jq.prototype, 'on');
69+
70+
linker($rootScope);
71+
72+
expect(jq.prototype.on).not.toHaveBeenCalled();
73+
});
74+
75+
76+
it('should not link and hookup an event if name is present at compile', function() {
77+
var jq = jQuery || jqLite;
78+
element = jq('<a name="bobby">hello@you</a>');
79+
var linker = $compile(element);
80+
81+
spyOn(jq.prototype, 'on');
82+
83+
linker($rootScope);
84+
85+
expect(jq.prototype.on).not.toHaveBeenCalled();
86+
});
6187
});

0 commit comments

Comments
 (0)