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

Commit a1e68d5

Browse files
crisbetokara
authored andcommitted
perf(chips,navbar,tooltip): avoid extra DOM lookups (#9527)
The chips, navbar and tooltip had a few cases where they look up all the elements of a certain class or node name, but they only use the first one. This change switches to using `querySelector` which will stop looking after the first match.
1 parent 08319e7 commit a1e68d5

File tree

6 files changed

+10
-10
lines changed

6 files changed

+10
-10
lines changed

src/components/chips/js/chipsController.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ MdChipsCtrl.prototype.updateChipContents = function(chipIndex, chipContents){
193193
* @return {boolean}
194194
*/
195195
MdChipsCtrl.prototype.isEditingChip = function() {
196-
return !!this.$element[0].getElementsByClassName('_md-chip-editing').length;
196+
return !!this.$element[0].querySelector('._md-chip-editing');
197197
};
198198

199199

@@ -215,7 +215,7 @@ MdChipsCtrl.prototype.isRemovable = function() {
215215
MdChipsCtrl.prototype.chipKeydown = function (event) {
216216
if (this.getChipBuffer()) return;
217217
if (this.isEditingChip()) return;
218-
218+
219219
switch (event.keyCode) {
220220
case this.$mdConstant.KEY_CODE.BACKSPACE:
221221
case this.$mdConstant.KEY_CODE.DELETE:

src/components/navBar/navBar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ function MdNavBarController($element, $scope, $timeout, $mdConstant) {
185185
* @private
186186
*/
187187
MdNavBarController.prototype._initTabs = function() {
188-
this._inkbar = angular.element(this._navBarEl.getElementsByTagName('md-nav-ink-bar')[0]);
188+
this._inkbar = angular.element(this._navBarEl.querySelector('md-nav-ink-bar'));
189189

190190
var self = this;
191191
this._$timeout(function() {

src/components/sticky/sticky.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('$mdSticky service', function() {
2626
} else {
2727
expect(contentEl.children().length).toBe(2);
2828

29-
var stickyClone = contentEl[0].getElementsByClassName('md-sticky-clone')[0];
29+
var stickyClone = contentEl[0].querySelector('.md-sticky-clone');
3030
expect(stickyClone).toBeTruthy();
3131

3232
expect(angular.element(stickyClone).scope()).toBe(scope);
@@ -68,7 +68,7 @@ describe('$mdSticky service', function() {
6868
} else {
6969
expect(contentEl.children().length).toBe(2);
7070

71-
var stickyClone = contentEl[0].getElementsByClassName('md-sticky-clone')[0];
71+
var stickyClone = contentEl[0].querySelector('.md-sticky-clone');
7272
expect(stickyClone).toBeTruthy();
7373

7474
expect(angular.element(stickyClone).scope()).toBe(cloneScope);

src/components/tooltip/tooltip.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function MdTooltipDirective($timeout, $window, $$rAF, $document, $mdUtil, $mdThe
6868
$mdTheming(element);
6969

7070
var parent = $mdUtil.getParentWithPointerEvents(element),
71-
content = angular.element(element[0].getElementsByClassName('md-content')[0]),
71+
content = angular.element(element[0].querySelector('.md-content')),
7272
tooltipParent = angular.element(document.body),
7373
showTimeout = null,
7474
debouncedOnResize = $$rAF.throttle(function () { updatePosition(); });

src/core/services/layout/layout.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ describe("Layout API ", function() {
33
describe("can be globally disabled with 'md-layouts-disabled' ", function() {
44
var disableLayouts = angular.noop,
55
activateLayouts = function() {
6-
var el = document.getElementsByTagName('body')[0];
6+
var el = document.body;
77
el.removeAttribute('md-layouts-disabled');
88

99
disableLayouts(false);
1010
};
1111

1212
beforeEach(function() {
13-
var el = document.getElementsByTagName('body')[0];
13+
var el = document.body;
1414
el.setAttribute('md-layouts-disabled', '');
1515

1616
// Load the core module

src/core/services/theming/theming.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,12 +582,12 @@ describe('$mdThemeProvider with disabled themes', function() {
582582
describe('can disable themes declaratively', function() {
583583
beforeEach(function() {
584584
// Set the body attribute BEFORE the theming module is loaded
585-
var el = document.getElementsByTagName('body')[0];
585+
var el = document.body;
586586
el.setAttribute('md-themes-disabled', '');
587587
});
588588

589589
afterEach(function() {
590-
var el = document.getElementsByTagName('body')[0];
590+
var el = document.body;
591591
el.removeAttribute('md-themes-disabled');
592592
});
593593

0 commit comments

Comments
 (0)