]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor(runtime-dom): avoid unnecessary typeof checks during props patching
authorEvan You <yyx990803@gmail.com>
Tue, 1 Dec 2020 01:05:02 +0000 (20:05 -0500)
committerEvan You <yyx990803@gmail.com>
Tue, 1 Dec 2020 01:05:02 +0000 (20:05 -0500)
packages/runtime-dom/src/modules/props.ts

index ab7a23a02f2ee1c337a9a76e2374d7520ef10453..068701642f8bd28d752c8e61096fd43c8213fbef 100644 (file)
@@ -24,6 +24,7 @@ export function patchDOMProp(
     el[key] = value == null ? '' : value
     return
   }
+
   if (key === 'value' && el.tagName !== 'PROGRESS') {
     // store value as _value as well since
     // non-string values will be stringified.
@@ -34,29 +35,36 @@ export function patchDOMProp(
     }
     return
   }
-  if (value === '' && typeof el[key] === 'boolean') {
-    // e.g. <select multiple> compiles to { multiple: '' }
-    el[key] = true
-  } else if (value == null && typeof el[key] === 'string') {
-    // e.g. <div :id="null">
-    el[key] = ''
-    el.removeAttribute(key)
-  } else if ((value == null || value === '') && typeof el[key] === 'number') {
-    // e.g. <img :width="null">
-    el[key] = 0
-    el.removeAttribute(key)
-  } else {
-    // some properties perform value validation and throw
-    try {
-      el[key] = value
-    } catch (e) {
-      if (__DEV__) {
-        warn(
-          `Failed setting prop "${key}" on <${el.tagName.toLowerCase()}>: ` +
-            `value ${value} is invalid.`,
-          e
-        )
-      }
+
+  if (value === '' || value == null) {
+    const type = typeof el[key]
+    if (value === '' && type === 'boolean') {
+      // e.g. <select multiple> compiles to { multiple: '' }
+      el[key] = true
+      return
+    } else if (value == null && type === 'string') {
+      // e.g. <div :id="null">
+      el[key] = ''
+      el.removeAttribute(key)
+      return
+    } else if (type === 'number') {
+      // e.g. <img :width="null">
+      el[key] = 0
+      el.removeAttribute(key)
+      return
+    }
+  }
+
+  // some properties perform value validation and throw
+  try {
+    el[key] = value
+  } catch (e) {
+    if (__DEV__) {
+      warn(
+        `Failed setting prop "${key}" on <${el.tagName.toLowerCase()}>: ` +
+          `value ${value} is invalid.`,
+        e
+      )
     }
   }
 }