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

Commit e4ba894

Browse files
jbedardrodyhaddad
authored andcommitted
perf(jqLite): expose the low-level jqLite.data/removeData calls
- updated the internal jqLite helpers to use the low-level jqLite.data/removeData to avoid unnecessary jq wrappers and loops - updated $compile to use the low-level jqLite.data/removeData to avoid unnecessary jq wrappers at link time
1 parent a160f76 commit e4ba894

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

src/jqLite.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -411,25 +411,22 @@ function jqLiteController(element, name) {
411411
}
412412

413413
function jqLiteInheritedData(element, name, value) {
414-
element = jqLite(element);
415-
416414
// if element is the document object work with the html element instead
417415
// this makes $(document).scope() possible
418-
if(element[0].nodeType == 9) {
419-
element = element.find('html');
416+
if(element.nodeType == 9) {
417+
element = element.documentElement;
420418
}
421419
var names = isArray(name) ? name : [name];
422420

423-
while (element.length) {
424-
var node = element[0];
421+
while (element) {
425422
for (var i = 0, ii = names.length; i < ii; i++) {
426-
if ((value = element.data(names[i])) !== undefined) return value;
423+
if ((value = jqLite.data(element, names[i])) !== undefined) return value;
427424
}
428425

429426
// If dealing with a document fragment node with a host element, and no parent, use the host
430427
// element as the parent. This enables directives within a Shadow DOM or polyfilled Shadow DOM
431428
// to lookup parent controllers.
432-
element = jqLite(node.parentNode || (node.nodeType === 11 && node.host));
429+
element = element.parentNode || (element.nodeType === 11 && element.host);
433430
}
434431
}
435432

@@ -512,18 +509,25 @@ function getAliasedAttrName(element, name) {
512509
return (nodeName === 'INPUT' || nodeName === 'TEXTAREA') && ALIASED_ATTR[name];
513510
}
514511

512+
forEach({
513+
data: jqLiteData,
514+
removeData: jqLiteRemoveData
515+
}, function(fn, name) {
516+
JQLite[name] = fn;
517+
});
518+
515519
forEach({
516520
data: jqLiteData,
517521
inheritedData: jqLiteInheritedData,
518522

519523
scope: function(element) {
520524
// Can't use jqLiteData here directly so we stay compatible with jQuery!
521-
return jqLite(element).data('$scope') || jqLiteInheritedData(element.parentNode || element, ['$isolateScope', '$scope']);
525+
return jqLite.data(element, '$scope') || jqLiteInheritedData(element.parentNode || element, ['$isolateScope', '$scope']);
522526
},
523527

524528
isolateScope: function(element) {
525529
// Can't use jqLiteData here directly so we stay compatible with jQuery!
526-
return jqLite(element).data('$isolateScope') || jqLite(element).data('$isolateScopeNoTemplate');
530+
return jqLite.data(element, '$isolateScope') || jqLite.data(element, '$isolateScopeNoTemplate');
527531
},
528532

529533
controller: jqLiteController,

src/ng/compile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
970970
if (nodeLinkFn) {
971971
if (nodeLinkFn.scope) {
972972
childScope = scope.$new();
973-
jqLite(node).data('$scope', childScope);
973+
jqLite.data(node, '$scope', childScope);
974974
} else {
975975
childScope = scope;
976976
}

test/jqLiteSpec.js

+22
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ describe('jqLite', function() {
386386
selected.removeData('prop2');
387387
});
388388

389+
389390
it('should add and remove data on SVGs', function() {
390391
var svg = jqLite('<svg><rect></rect></svg>');
391392

@@ -415,6 +416,27 @@ describe('jqLite', function() {
415416
});
416417

417418

419+
it('should provide the non-wrapped data calls', function() {
420+
var node = document.createElement('div');
421+
422+
expect(jqLite.data(node, "foo")).toBeUndefined();
423+
424+
jqLite.data(node, "foo", "bar");
425+
426+
expect(jqLite.data(node, "foo")).toBe("bar");
427+
expect(jqLite(node).data("foo")).toBe("bar");
428+
429+
expect(jqLite.data(node)).toBe(jqLite(node).data());
430+
431+
jqLite.removeData(node, "foo");
432+
expect(jqLite.data(node, "foo")).toBeUndefined();
433+
434+
jqLite.data(node, "bar", "baz");
435+
jqLite.removeData(node);
436+
jqLite.removeData(node);
437+
expect(jqLite.data(node, "bar")).toBeUndefined();
438+
});
439+
418440
it('should emit $destroy event if element removed via remove()', function() {
419441
var log = '';
420442
var element = jqLite(a);

0 commit comments

Comments
 (0)