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

[performance] Remove hasOwnProperty calls to objects that are managed within React #5504

Closed
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
2 changes: 1 addition & 1 deletion src/addons/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function update(value, spec) {
}

for (var k in spec) {
if (!(ALL_COMMANDS_SET.hasOwnProperty(k) && ALL_COMMANDS_SET[k])) {
if (!ALL_COMMANDS_SET[k]) {
nextValue[k] = update(value[k], spec[k]);
}
}
Expand Down
20 changes: 9 additions & 11 deletions src/isomorphic/classic/class/ReactClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,12 +415,12 @@ function validateTypeDef(Constructor, typeDef, location) {
}

function validateMethodOverride(proto, name) {
var specPolicy = ReactClassInterface.hasOwnProperty(name) ?
var specPolicy = ReactClassInterface[name] ?
ReactClassInterface[name] :
null;

// Disallow overriding of base class methods unless explicitly allowed.
if (ReactClassMixin.hasOwnProperty(name)) {
if (ReactClassMixin[name]) {
invariant(
specPolicy === SpecPolicy.OVERRIDE_BASE,
'ReactClassInterface: You are attempting to override ' +
Expand Down Expand Up @@ -486,15 +486,15 @@ function mixSpecIntoComponent(Constructor, spec) {
var property = spec[name];
validateMethodOverride(proto, name);

if (RESERVED_SPEC_KEYS.hasOwnProperty(name)) {
if (RESERVED_SPEC_KEYS[name]) {
RESERVED_SPEC_KEYS[name](Constructor, property);
} else {
// Setup methods on prototype:
// The following member methods should not be automatically bound:
// 1. Expected ReactClass methods (in the "interface").
// 2. Overridden methods (that were mixed in).
var isReactClassMethod =
ReactClassInterface.hasOwnProperty(name);
!!ReactClassInterface[name];
var isAlreadyDefined = proto.hasOwnProperty(name);
var isFunction = typeof property === 'function';
var shouldAutoBind =
Expand Down Expand Up @@ -701,13 +701,11 @@ function bindAutoBindMethod(component, method) {
*/
function bindAutoBindMethods(component) {
for (var autoBindKey in component.__reactAutoBindMap) {
if (component.__reactAutoBindMap.hasOwnProperty(autoBindKey)) {
var method = component.__reactAutoBindMap[autoBindKey];
component[autoBindKey] = bindAutoBindMethod(
component,
method
);
}
var method = component.__reactAutoBindMap[autoBindKey];
component[autoBindKey] = bindAutoBindMethod(
component,
method
);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/isomorphic/classic/element/ReactElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ ReactElement.createElement = function(type, config, children) {
// Remaining properties are added to a new props object
for (propName in config) {
if (config.hasOwnProperty(propName) &&
!RESERVED_PROPS.hasOwnProperty(propName)) {
!RESERVED_PROPS[propName]) {
props[propName] = config[propName];
}
}
Expand Down Expand Up @@ -243,7 +243,7 @@ ReactElement.cloneElement = function(element, config, children) {
// Remaining properties override existing props
for (propName in config) {
if (config.hasOwnProperty(propName) &&
!RESERVED_PROPS.hasOwnProperty(propName)) {
!RESERVED_PROPS[propName]) {
props[propName] = config[propName];
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/renderers/dom/shared/DOMProperty.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ var DOMPropertyInjection = {

for (var propName in Properties) {
invariant(
!DOMProperty.properties.hasOwnProperty(propName),
!DOMProperty.properties[propName],
'injectDOMPropertyConfig(...): You\'re trying to inject DOM property ' +
'\'%s\' which has already been injected. You may be accidentally ' +
'injecting the same DOM property config twice, or you may be ' +
Expand Down Expand Up @@ -125,23 +125,23 @@ var DOMPropertyInjection = {
DOMProperty.getPossibleStandardName[lowerCased] = propName;
}

if (DOMAttributeNames.hasOwnProperty(propName)) {
if (DOMAttributeNames[propName]) {
var attributeName = DOMAttributeNames[propName];
propertyInfo.attributeName = attributeName;
if (__DEV__) {
DOMProperty.getPossibleStandardName[attributeName] = propName;
}
}

if (DOMAttributeNamespaces.hasOwnProperty(propName)) {
if (DOMAttributeNamespaces[propName]) {
propertyInfo.attributeNamespace = DOMAttributeNamespaces[propName];
}

if (DOMPropertyNames.hasOwnProperty(propName)) {
if (DOMPropertyNames[propName]) {
propertyInfo.propertyName = DOMPropertyNames[propName];
}

if (DOMMutationMethods.hasOwnProperty(propName)) {
if (DOMMutationMethods[propName]) {
propertyInfo.mutationMethod = DOMMutationMethods[propName];
}

Expand Down
29 changes: 9 additions & 20 deletions src/renderers/dom/shared/DOMPropertyOperations.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ var illegalAttributeNameCache = {};
var validatedAttributeNameCache = {};

function isAttributeNameSafe(attributeName) {
if (validatedAttributeNameCache.hasOwnProperty(attributeName)) {
if (validatedAttributeNameCache[attributeName]) {
return true;
}
if (illegalAttributeNameCache.hasOwnProperty(attributeName)) {
if (illegalAttributeNameCache[attributeName]) {
return false;
}
if (VALID_ATTRIBUTE_NAME_REGEX.test(attributeName)) {
Expand Down Expand Up @@ -62,8 +62,7 @@ if (__DEV__) {
var warnedProperties = {};

var warnUnknownProperty = function(name) {
if (reactProps.hasOwnProperty(name) && reactProps[name] ||
warnedProperties.hasOwnProperty(name) && warnedProperties[name]) {
if (reactProps[name] || warnedProperties[name]) {
return;
}

Expand All @@ -74,9 +73,7 @@ if (__DEV__) {
var standardName = (
DOMProperty.isCustomAttribute(lowerCasedName) ?
lowerCasedName :
DOMProperty.getPossibleStandardName.hasOwnProperty(lowerCasedName) ?
DOMProperty.getPossibleStandardName[lowerCasedName] :
null
DOMProperty.getPossibleStandardName[lowerCasedName] || null
);

// For now, only warn when we have a suggested correction. This prevents
Expand All @@ -88,13 +85,8 @@ if (__DEV__) {
standardName
);

var registrationName = (
EventPluginRegistry.possibleRegistrationNames.hasOwnProperty(
lowerCasedName
) ?
EventPluginRegistry.possibleRegistrationNames[lowerCasedName] :
null
);
var registrationName =
EventPluginRegistry.possibleRegistrationNames[lowerCasedName] || null;

warning(
registrationName == null,
Expand Down Expand Up @@ -141,8 +133,7 @@ var DOMPropertyOperations = {
* @return {?string} Markup string, or null if the property was invalid.
*/
createMarkupForProperty: function(name, value) {
var propertyInfo = DOMProperty.properties.hasOwnProperty(name) ?
DOMProperty.properties[name] : null;
var propertyInfo = DOMProperty.properties[name] || null;
if (propertyInfo) {
if (shouldIgnoreValue(propertyInfo, value)) {
return '';
Expand Down Expand Up @@ -186,8 +177,7 @@ var DOMPropertyOperations = {
* @param {*} value
*/
setValueForProperty: function(node, name, value) {
var propertyInfo = DOMProperty.properties.hasOwnProperty(name) ?
DOMProperty.properties[name] : null;
var propertyInfo = DOMProperty.properties[name] || null;
if (propertyInfo) {
var mutationMethod = propertyInfo.mutationMethod;
if (mutationMethod) {
Expand Down Expand Up @@ -243,8 +233,7 @@ var DOMPropertyOperations = {
* @param {string} name
*/
deleteValueForProperty: function(node, name) {
var propertyInfo = DOMProperty.properties.hasOwnProperty(name) ?
DOMProperty.properties[name] : null;
var propertyInfo = DOMProperty.properties[name] || null;
if (propertyInfo) {
var mutationMethod = propertyInfo.mutationMethod;
if (mutationMethod) {
Expand Down
27 changes: 12 additions & 15 deletions src/renderers/dom/shared/ReactDOMComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ function checkAndWarnForMutatedStyle(style1, style2, component) {

var hash = ownerName + '|' + componentName;

if (styleMutationWarning.hasOwnProperty(hash)) {
if (styleMutationWarning[hash]) {
return;
}

Expand Down Expand Up @@ -378,15 +378,13 @@ function trapBubbledEventsLocal() {
inst._wrapperState.listeners = [];
// Create listener for each media event
for (var event in mediaEvents) {
if (mediaEvents.hasOwnProperty(event)) {
inst._wrapperState.listeners.push(
ReactBrowserEventEmitter.trapBubbledEvent(
EventConstants.topLevelTypes[event],
mediaEvents[event],
node
)
);
}
inst._wrapperState.listeners.push(
ReactBrowserEventEmitter.trapBubbledEvent(
EventConstants.topLevelTypes[event],
mediaEvents[event],
node
)
);
}

break;
Expand Down Expand Up @@ -481,10 +479,9 @@ var voidElementTags = assign({

var VALID_TAG_REGEX = /^[a-zA-Z][a-zA-Z:_\.\-\d]*$/; // Simplified subset
var validatedTagCache = {};
var hasOwnProperty = {}.hasOwnProperty;

function validateDangerousTag(tag) {
if (!hasOwnProperty.call(validatedTagCache, tag)) {
if (!validatedTagCache[tag]) {
invariant(VALID_TAG_REGEX.test(tag), 'Invalid tag: %s', tag);
validatedTagCache[tag] = true;
}
Expand Down Expand Up @@ -726,7 +723,7 @@ ReactDOMComponent.Mixin = {
if (propValue == null) {
continue;
}
if (registrationNameModules.hasOwnProperty(propKey)) {
if (registrationNameModules[propKey]) {
if (propValue) {
enqueuePutListener(this, propKey, propValue, transaction);
}
Expand Down Expand Up @@ -954,7 +951,7 @@ ReactDOMComponent.Mixin = {
}
}
this._previousStyleCopy = null;
} else if (registrationNameModules.hasOwnProperty(propKey)) {
} else if (registrationNameModules[propKey]) {
if (lastProps[propKey]) {
// Only call deleteListener if there was a listener previously or
// else willDeleteListener gets called when there wasn't actually a
Expand Down Expand Up @@ -1012,7 +1009,7 @@ ReactDOMComponent.Mixin = {
// Relies on `updateStylesByID` not mutating `styleUpdates`.
styleUpdates = nextProp;
}
} else if (registrationNameModules.hasOwnProperty(propKey)) {
} else if (registrationNameModules[propKey]) {
if (nextProp) {
enqueuePutListener(this, propKey, nextProp, transaction);
} else if (lastProp) {
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/shared/event/EventPluginRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function recomputePluginOrdering() {
*/
function publishEventForPlugin(dispatchConfig, PluginModule, eventName) {
invariant(
!EventPluginRegistry.eventNameDispatchConfigs.hasOwnProperty(eventName),
!EventPluginRegistry.eventNameDispatchConfigs[eventName],
'EventPluginHub: More than one plugin attempted to publish the same ' +
'event name, `%s`.',
eventName
Expand Down