Performance: v-model modifiers (lazy, number, trim)
These modifiers affect how and when input data updates the Vue component state, impacting interaction responsiveness and rendering frequency.
<input v-model.lazy="text" /><input v-model="text" />| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| v-model without modifiers | Updates on every input event | Triggers reflow per keystroke | High paint cost due to frequent updates | [X] Bad |
| v-model.lazy | Updates on change event only | Single reflow per input blur | Lower paint cost | [OK] Good |
| v-model.number | Converts input to number on update | No extra reflows | Minimal paint cost | [OK] Good |
| v-model.trim | Trims whitespace before update | No extra reflows | Minimal paint cost | [OK] Good |
.lazy modifier do when used with v-model in Vue?v-model behaviorv-model updates the bound data on every input event (each keystroke)..lazy modifier.lazy modifier changes this behavior to update only when the input loses focus or on the change event..lazy delays update until change [OK]v-model with the .number modifier on an input element?v-model with a dot, like v-model.number.<input v-model.number="age">. Placing the modifier inside quotes, using a function call, or adding it as a separate attribute is incorrect.<input v-model.trim="username">
<p>User: {{ username }}</p> alice (with spaces before and after) and then the input loses focus, what will be displayed inside the paragraph?.trim modifier effect.trim modifier removes whitespace from the start and end of the input value before updating the bound data. alice will be trimmed to alice before updating username..trim removes spaces around input [OK]<input v-model.number.lazy="score">
42 but the bound score variable remains a string instead of a number?.lazy event timing.lazy modifier updates the bound data only on the change event (when input loses focus), not on every input event..number conversion timing.number modifier converts the value when the update happens. If the update hasn't happened yet (input still focused), score remains unchanged..lazy delays update, so number conversion waits [OK]v-model usage?.lazy modifier must come first to delay update until change event..trim and .number after .lazy.trim removes spaces, then .number converts to number before setting the variable.Pass the test and this pattern moves unknown