[{"data":1,"prerenderedAt":569},["ShallowReactive",2],{"blog-en-\u002Fblog\u002Fvue-2-computed-vs-methods":3},{"id":4,"title":5,"body":6,"date":556,"description":557,"extension":558,"meta":559,"navigation":560,"path":561,"seo":562,"stem":563,"tags":564,"__hash__":568},"blog_en\u002Fblog\u002Fvue-2-computed-vs-methods.md","Vue 2: when to use computed and when to use methods",{"type":7,"value":8,"toc":550},"minimark",[9,13,18,26,30,33,59,63,84,87,317,332,508,533,537,546],[10,11,12],"p",{},"A simple Vue 2 question that taught me to think more carefully about derived state, caching and responsibility inside a component.",[14,15,17],"h2",{"id":16},"the-question","The question",[10,19,20,21,25],{},"While building interfaces with Vue 2, I noticed that it was very easy to put any logic inside ",[22,23,24],"code",{},"methods",". It worked, but it didn't always express correctly what that logic represented. The question was: if a function only calculates a value from the current state, should it really be a method?",[14,27,29],{"id":28},"what-i-understood","What I understood",[10,31,32],{},"A computed property describes a derived value. It tracks its reactive dependencies and reuses the result as long as those dependencies don't change. A method, on the other hand, runs again every time it is called during rendering.",[10,34,35,36,39,40,43,44,47,48,51,52,54,55,58],{},"The difference isn't only performance. It's also semantics. If ",[22,37,38],{},"fullName"," depends on ",[22,41,42],{},"firstName"," and ",[22,45,46],{},"lastName",", ",[22,49,50],{},"computed"," communicates that ",[22,53,38],{}," is part of the screen's derived state. ",[22,56,57],{},"submitForm",", by contrast, is an action and makes more sense as a method.",[14,60,62],{"id":61},"a-simple-example","A simple example",[10,64,65,66,43,68,70,71,74,75,77,78,80,81,83],{},"In a form, I can keep ",[22,67,42],{},[22,69,46],{}," in ",[22,72,73],{},"data"," and declare ",[22,76,38],{}," as a ",[22,79,50],{},". If either one changes, Vue recalculates the full name. To validate and submit the form, I use ",[22,82,24],{},", because there is an explicit action and possibly a side effect.",[10,85,86],{},"Before I understood the difference well, this same form looked like this:",[88,89,94],"pre",{"className":90,"code":91,"language":92,"meta":93,"style":93},"language-js shiki shiki-themes darcula","export default {\n  data() {\n    return {\n      firstName: '',\n      lastName: ''\n    }\n  },\n  methods: {\n    fullName() {\n      return `${this.firstName} ${this.lastName}`.trim()\n    },\n    submitForm() {\n      if (!this.fullName()) return\n      this.$emit('submit', { fullName: this.fullName() })\n    }\n  }\n}\n","js","",[22,95,96,112,122,130,147,158,164,170,179,187,232,238,246,267,300,305,311],{"__ignoreMap":93},[97,98,101,105,108],"span",{"class":99,"line":100},"line",1,[97,102,104],{"class":103},"sYMgc","export",[97,106,107],{"class":103}," default",[97,109,111],{"class":110},"sE2CK"," {\n",[97,113,115,119],{"class":99,"line":114},2,[97,116,118],{"class":117},"s2lgc","  data",[97,120,121],{"class":110},"() {\n",[97,123,125,128],{"class":99,"line":124},3,[97,126,127],{"class":103},"    return",[97,129,111],{"class":110},[97,131,133,137,140,144],{"class":99,"line":132},4,[97,134,136],{"class":135},"sk7qV","      firstName",[97,138,139],{"class":110},": ",[97,141,143],{"class":142},"sZ3PO","''",[97,145,146],{"class":110},",\n",[97,148,150,153,155],{"class":99,"line":149},5,[97,151,152],{"class":135},"      lastName",[97,154,139],{"class":110},[97,156,157],{"class":142},"''\n",[97,159,161],{"class":99,"line":160},6,[97,162,163],{"class":110},"    }\n",[97,165,167],{"class":99,"line":166},7,[97,168,169],{"class":110},"  },\n",[97,171,173,176],{"class":99,"line":172},8,[97,174,175],{"class":135},"  methods",[97,177,178],{"class":110},": {\n",[97,180,182,185],{"class":99,"line":181},9,[97,183,184],{"class":117},"    fullName",[97,186,121],{"class":110},[97,188,190,193,196,199,202,205,207,210,213,215,217,219,221,224,226,229],{"class":99,"line":189},10,[97,191,192],{"class":103},"      return",[97,194,195],{"class":142}," `",[97,197,198],{"class":103},"${",[97,200,201],{"class":103},"this",[97,203,204],{"class":110},".",[97,206,42],{"class":135},[97,208,209],{"class":103},"}",[97,211,212],{"class":103}," ${",[97,214,201],{"class":103},[97,216,204],{"class":110},[97,218,46],{"class":135},[97,220,209],{"class":103},[97,222,223],{"class":142},"`",[97,225,204],{"class":110},[97,227,228],{"class":117},"trim",[97,230,231],{"class":110},"()\n",[97,233,235],{"class":99,"line":234},11,[97,236,237],{"class":110},"    },\n",[97,239,241,244],{"class":99,"line":240},12,[97,242,243],{"class":117},"    submitForm",[97,245,121],{"class":110},[97,247,249,252,255,257,259,261,264],{"class":99,"line":248},13,[97,250,251],{"class":103},"      if",[97,253,254],{"class":110}," (!",[97,256,201],{"class":103},[97,258,204],{"class":110},[97,260,38],{"class":117},[97,262,263],{"class":110},"()) ",[97,265,266],{"class":103},"return\n",[97,268,270,273,275,278,281,284,287,289,291,293,295,297],{"class":99,"line":269},14,[97,271,272],{"class":103},"      this",[97,274,204],{"class":110},[97,276,277],{"class":117},"$emit",[97,279,280],{"class":110},"(",[97,282,283],{"class":142},"'submit'",[97,285,286],{"class":110},", { ",[97,288,38],{"class":135},[97,290,139],{"class":110},[97,292,201],{"class":103},[97,294,204],{"class":110},[97,296,38],{"class":117},[97,298,299],{"class":110},"() })\n",[97,301,303],{"class":99,"line":302},15,[97,304,163],{"class":110},[97,306,308],{"class":99,"line":307},16,[97,309,310],{"class":110},"  }\n",[97,312,314],{"class":99,"line":313},17,[97,315,316],{"class":110},"}\n",[10,318,319,320,323,324,43,326,328,329,331],{},"In the template, that becomes ",[22,321,322],{},"{{ fullName() }}"," — a function call that re-runs on every render, even when ",[22,325,42],{},[22,327,46],{}," haven't changed. With ",[22,330,50],{},", the same component looks like this:",[88,333,335],{"className":90,"code":334,"language":92,"meta":93,"style":93},"export default {\n  data() {\n    return {\n      firstName: '',\n      lastName: ''\n    }\n  },\n  computed: {\n    fullName() {\n      return `${this.firstName} ${this.lastName}`.trim()\n    }\n  },\n  methods: {\n    submitForm() {\n      if (!this.fullName) return\n      this.$emit('submit', { fullName: this.fullName })\n    }\n  }\n}\n",[22,336,337,345,351,357,367,375,379,383,390,396,430,434,438,444,450,467,494,498,503],{"__ignoreMap":93},[97,338,339,341,343],{"class":99,"line":100},[97,340,104],{"class":103},[97,342,107],{"class":103},[97,344,111],{"class":110},[97,346,347,349],{"class":99,"line":114},[97,348,118],{"class":117},[97,350,121],{"class":110},[97,352,353,355],{"class":99,"line":124},[97,354,127],{"class":103},[97,356,111],{"class":110},[97,358,359,361,363,365],{"class":99,"line":132},[97,360,136],{"class":135},[97,362,139],{"class":110},[97,364,143],{"class":142},[97,366,146],{"class":110},[97,368,369,371,373],{"class":99,"line":149},[97,370,152],{"class":135},[97,372,139],{"class":110},[97,374,157],{"class":142},[97,376,377],{"class":99,"line":160},[97,378,163],{"class":110},[97,380,381],{"class":99,"line":166},[97,382,169],{"class":110},[97,384,385,388],{"class":99,"line":172},[97,386,387],{"class":135},"  computed",[97,389,178],{"class":110},[97,391,392,394],{"class":99,"line":181},[97,393,184],{"class":117},[97,395,121],{"class":110},[97,397,398,400,402,404,406,408,410,412,414,416,418,420,422,424,426,428],{"class":99,"line":189},[97,399,192],{"class":103},[97,401,195],{"class":142},[97,403,198],{"class":103},[97,405,201],{"class":103},[97,407,204],{"class":110},[97,409,42],{"class":135},[97,411,209],{"class":103},[97,413,212],{"class":103},[97,415,201],{"class":103},[97,417,204],{"class":110},[97,419,46],{"class":135},[97,421,209],{"class":103},[97,423,223],{"class":142},[97,425,204],{"class":110},[97,427,228],{"class":117},[97,429,231],{"class":110},[97,431,432],{"class":99,"line":234},[97,433,163],{"class":110},[97,435,436],{"class":99,"line":240},[97,437,169],{"class":110},[97,439,440,442],{"class":99,"line":248},[97,441,175],{"class":135},[97,443,178],{"class":110},[97,445,446,448],{"class":99,"line":269},[97,447,243],{"class":117},[97,449,121],{"class":110},[97,451,452,454,456,458,460,462,465],{"class":99,"line":302},[97,453,251],{"class":103},[97,455,254],{"class":110},[97,457,201],{"class":103},[97,459,204],{"class":110},[97,461,38],{"class":135},[97,463,464],{"class":110},") ",[97,466,266],{"class":103},[97,468,469,471,473,475,477,479,481,483,485,487,489,491],{"class":99,"line":307},[97,470,272],{"class":103},[97,472,204],{"class":110},[97,474,277],{"class":117},[97,476,280],{"class":110},[97,478,283],{"class":142},[97,480,286],{"class":110},[97,482,38],{"class":135},[97,484,139],{"class":110},[97,486,201],{"class":103},[97,488,204],{"class":110},[97,490,38],{"class":135},[97,492,493],{"class":110}," })\n",[97,495,496],{"class":99,"line":313},[97,497,163],{"class":110},[97,499,501],{"class":99,"line":500},18,[97,502,310],{"class":110},[97,504,506],{"class":99,"line":505},19,[97,507,316],{"class":110},[10,509,510,511,513,514,517,518,520,521,523,524,526,527,529,530,532],{},"Now ",[22,512,38],{}," is accessed as a property in the template (",[22,515,516],{},"{{ fullName }}",", no parentheses) and only recalculates when ",[22,519,42],{}," or ",[22,522,46],{}," change — the result stays cached between renders. ",[22,525,57],{}," stays in ",[22,528,24],{},", because it represents an action with a side effect (",[22,531,277],{},"), not a derived value.",[14,534,536],{"id":535},"conclusion-for-now","Conclusion for now",[10,538,539,540,542,543,545],{},"This small distinction improved the readability of my components. I started asking whether the code represents a value or an action. When it represents a derived value, ",[22,541,50],{}," is usually the first option; when it represents behavior, ",[22,544,24],{}," tends to be clearer.",[547,548,549],"style",{},"html pre.shiki code .sYMgc, html code.shiki .sYMgc{--shiki-default:#CC7832}html pre.shiki code .sE2CK, html code.shiki .sE2CK{--shiki-default:#A9B7C6}html pre.shiki code .s2lgc, html code.shiki .s2lgc{--shiki-default:#FFC66D}html pre.shiki code .sk7qV, html code.shiki .sk7qV{--shiki-default:#9876AA}html pre.shiki code .sZ3PO, html code.shiki .sZ3PO{--shiki-default:#6A8759}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":93,"searchDepth":114,"depth":114,"links":551},[552,553,554,555],{"id":16,"depth":114,"text":17},{"id":28,"depth":114,"text":29},{"id":61,"depth":114,"text":62},{"id":535,"depth":114,"text":536},"2021-11-18","The difference between computed and methods in Vue 2: a cached derived value versus an action that runs on every call, and what that says about a component.","md",{},true,"\u002Fblog\u002Fvue-2-computed-vs-methods",{"title":5,"description":557},"blog\u002Fvue-2-computed-vs-methods",[565,566,567],"vue","javascript","fundamentals","8TZ_EcEdWBkO5_2QxEaR17vrzbjJf0Jf3ji7iFisw-s",1794928450501]