Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove object allocation in update of single property of multi-prop component #3812

Merged
merged 1 commit into from
Oct 18, 2018
Merged
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
91 changes: 49 additions & 42 deletions src/core/a-entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,52 +622,59 @@ var proto = Object.create(ANode.prototype, {
* it is a boolean indicating whether to clobber previous values (defaults to false).
*/
setAttribute: {
value: function (attrName, arg1, arg2) {
var newAttrValue;
var clobber;
var componentName;
var delimiterIndex;
var isDebugMode;

delimiterIndex = attrName.indexOf(MULTIPLE_COMPONENT_DELIMITER);
componentName = delimiterIndex > 0 ? attrName.substring(0, delimiterIndex) : attrName;

// Not a component. Normal set attribute.
if (!COMPONENTS[componentName]) {
if (attrName === 'mixin') { this.mixinUpdate(arg1); }
ANode.prototype.setAttribute.call(this, attrName, arg1);
return;
}
value: (function () {
var singlePropUpdate = {};

return function (attrName, arg1, arg2) {
var newAttrValue;
var clobber;
var componentName;
var delimiterIndex;
var isDebugMode;
var key;

delimiterIndex = attrName.indexOf(MULTIPLE_COMPONENT_DELIMITER);
componentName = delimiterIndex > 0 ? attrName.substring(0, delimiterIndex) : attrName;

// Not a component. Normal set attribute.
if (!COMPONENTS[componentName]) {
if (attrName === 'mixin') { this.mixinUpdate(arg1); }
ANode.prototype.setAttribute.call(this, attrName, arg1);
return;
}

// Initialize component first if not yet initialized.
if (!this.components[attrName] && this.hasAttribute(attrName)) {
this.updateComponent(attrName,
window.HTMLElement.prototype.getAttribute.call(this, attrName));
}
// Initialize component first if not yet initialized.
if (!this.components[attrName] && this.hasAttribute(attrName)) {
this.updateComponent(
attrName,
window.HTMLElement.prototype.getAttribute.call(this, attrName));
}

// Determine new attributes from the arguments
if (typeof arg2 !== 'undefined' &&
typeof arg1 === 'string' &&
arg1.length > 0 &&
typeof utils.styleParser.parse(arg1) === 'string') {
// Update a single property of a multi-property component
newAttrValue = {};
newAttrValue[arg1] = arg2;
clobber = false;
} else {
// Update with a value, object, or CSS-style property string, with the possiblity
// of clobbering previous values.
newAttrValue = arg1;
clobber = (arg2 === true);
}
// Determine new attributes from the arguments
if (typeof arg2 !== 'undefined' &&
typeof arg1 === 'string' &&
arg1.length > 0 &&
typeof utils.styleParser.parse(arg1) === 'string') {
// Update a single property of a multi-property component
for (key in singlePropUpdate) { delete singlePropUpdate[key]; }
newAttrValue = singlePropUpdate;
newAttrValue[arg1] = arg2;
clobber = false;
} else {
// Update with a value, object, or CSS-style property string, with the possiblity
// of clobbering previous values.
newAttrValue = arg1;
clobber = (arg2 === true);
}

// Update component
this.updateComponent(attrName, newAttrValue, clobber);
// Update component
this.updateComponent(attrName, newAttrValue, clobber);

// In debug mode, write component data up to the DOM.
isDebugMode = this.sceneEl && this.sceneEl.getAttribute('debug');
if (isDebugMode) { this.components[attrName].flushToDOM(); }
},
// In debug mode, write component data up to the DOM.
isDebugMode = this.sceneEl && this.sceneEl.getAttribute('debug');
if (isDebugMode) { this.components[attrName].flushToDOM(); }
};
})(),
writable: window.debug
},

Expand Down