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

perf($compile, jqLite): reduce creation of unnecessary jquery objects #7963

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,25 +409,22 @@ function jqLiteController(element, name) {
}

function jqLiteInheritedData(element, name, value) {
element = jqLite(element);

// if element is the document object work with the html element instead
// this makes $(document).scope() possible
if(element[0].nodeType == 9) {
element = element.find('html');
if(element.nodeType == 9) {
element = element.documentElement;
}
var names = isArray(name) ? name : [name];

while (element.length) {
var node = element[0];
while (element) {
for (var i = 0, ii = names.length; i < ii; i++) {
if ((value = element.data(names[i])) !== undefined) return value;
if ((value = jqLite.data(element, names[i])) !== undefined) return value;
}

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

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

forEach({
data: jqLiteData,
removeData: jqLiteRemoveData
}, function(fn, name) {
JQLite[name] = fn;
});

forEach({
data: jqLiteData,
inheritedData: jqLiteInheritedData,

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

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

controller: jqLiteController,
Expand Down
18 changes: 8 additions & 10 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
: null;

if (nodeLinkFn && nodeLinkFn.scope) {
safeAddClass(jqLite(nodeList[i]), 'ng-scope');
safeAddClass(attrs.$$element, 'ng-scope');
}

childLinkFn = (nodeLinkFn && nodeLinkFn.terminal ||
Expand All @@ -944,7 +944,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
return linkFnFound ? compositeLinkFn : null;

function compositeLinkFn(scope, nodeList, $rootElement, parentBoundTranscludeFn) {
var nodeLinkFn, childLinkFn, node, $node, childScope, i, ii, n, childBoundTranscludeFn;
var nodeLinkFn, childLinkFn, node, childScope, i, ii, n, childBoundTranscludeFn;

// copy nodeList so that linking doesn't break due to live list updates.
var nodeListLength = nodeList.length,
Expand All @@ -957,12 +957,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
node = stableNodeList[n];
nodeLinkFn = linkFns[i++];
childLinkFn = linkFns[i++];
$node = jqLite(node);

if (nodeLinkFn) {
if (nodeLinkFn.scope) {
childScope = scope.$new();
$node.data('$scope', childScope);
jqLite.data(node, '$scope', childScope);
} else {
childScope = scope;
}
Expand Down Expand Up @@ -1262,12 +1261,12 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
if (directiveValue == 'element') {
hasElementTranscludeDirective = true;
terminalPriority = directive.priority;
$template = groupScan(compileNode, attrStart, attrEnd);
$template = $compileNode;
$compileNode = templateAttrs.$$element =
jqLite(document.createComment(' ' + directiveName + ': ' +
templateAttrs[directiveName] + ' '));
compileNode = $compileNode[0];
replaceWith(jqCollection, jqLite(sliceArgs($template)), compileNode);
replaceWith(jqCollection, sliceArgs($template), compileNode);

childTranscludeFn = compile($template, transcludeFn, terminalPriority,
replaceDirective && replaceDirective.name, {
Expand Down Expand Up @@ -1453,20 +1452,19 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {

if (newIsolateScopeDirective) {
var LOCAL_REGEXP = /^\s*([@=&])(\??)\s*(\w*)\s*$/;
var $linkNode = jqLite(linkNode);

isolateScope = scope.$new(true);

if (templateDirective && (templateDirective === newIsolateScopeDirective ||
templateDirective === newIsolateScopeDirective.$$originalDirective)) {
$linkNode.data('$isolateScope', isolateScope) ;
$element.data('$isolateScope', isolateScope) ;
} else {
$linkNode.data('$isolateScopeNoTemplate', isolateScope);
$element.data('$isolateScopeNoTemplate', isolateScope);
}



safeAddClass($linkNode, 'ng-isolate-scope');
safeAddClass($element, 'ng-isolate-scope');

forEach(newIsolateScopeDirective.scope, function(definition, scopeName) {
var match = definition.match(LOCAL_REGEXP) || [],
Expand Down
20 changes: 20 additions & 0 deletions test/jqLiteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,26 @@ describe('jqLite', function() {
selected.removeData('prop2');
});

it('should provide the non-wrapped data calls', function() {
var node = document.createElement('div');

expect(jqLite.data(node, "foo")).toBeUndefined();

jqLite.data(node, "foo", "bar");

expect(jqLite.data(node, "foo")).toBe("bar");
expect(jqLite(node).data("foo")).toBe("bar");

expect(jqLite.data(node)).toBe(jqLite(node).data());

jqLite.removeData(node, "foo");
expect(jqLite.data(node, "foo")).toBeUndefined();

jqLite.data(node, "bar", "baz");
jqLite.removeData(node);
jqLite.removeData(node);
expect(jqLite.data(node, "bar")).toBeUndefined();
});

it('should not add to the cache if the node is a comment or text node', function() {
var calcCacheSize = function() {
Expand Down