Performance: Global properties and methods
Global properties and methods affect initial app load and runtime performance by adding shared reactive or non-reactive data accessible across components.
import { heavyFunction } from './utils'; // Use heavyFunction only where needed inside components
app.config.globalProperties.$utils = { heavyFunction: () => { /* large code */ } }| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Large globalProperties object | 0 (no DOM changes) | 0 | 0 | [!] OK but costly in JS execution and memory |
| Scoped imports in components | 0 | 0 | 0 | [OK] Minimal JS overhead, better load performance |
app.config.globalProperties in Vue?app.config.globalPropertiesglobalProperties.logMessage in Vue 3?app.config.globalProperties with a $ prefix on the property name.app.config.globalProperties.$logMessage with an arrow function.app.config.globalProperties.$name for globals [OK]app.config.globalProperties.$greet = (name) => `Hello, ${name}!`
What will be the output of this code inside a component method?
console.log(this.$greet('Alice'))$greet is defined globally to return a greeting string with the passed name.this.$greet('Alice') invokes the global method, returning Hello, Alice!.app.config.globalProperties.log = function(message) {
console.log(message)
}
// Usage in component:
this.$log('Test')log but used as $log in the component, causing mismatch.$ prefix to match usage in components.$formatDate that formats a Date object to YYYY-MM-DD. Which is the best way to define and use it in Vue 3?app.config.globalProperties with a $ prefix and accessed via this.$name in components.app.config.globalProperties.$formatDate as a function returning formatted string; call with this.$formatDate(date) in components correctly defines the formatter globally and uses it properly in components, promoting code reuse.Pass the test and this pattern moves unknown