{"componentChunkName":"component---src-templates-blog-post-js","path":"/computed-properties-what-are-they/","webpackCompilationHash":"23268da9a2d41a689f81","result":{"data":{"site":{"siteMetadata":{"title":"A Vue Blog","author":"Gautham Lal"}},"markdownRemark":{"id":"f8ebd120-fb94-5b46-b00b-c914efcbf946","excerpt":"Let’s go back to the very first blog post where we talked about templates. Another useful thing about templates is that you can write simple JavaScript…","html":"<p>Let’s go back to the <a href=\"https://avueblog.netlify.com/hello-world/\">very first blog post</a> where we talked about templates. Another useful thing about templates is that you can write simple JavaScript expressions in them.</p>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>div</span><span class=\"token punctuation\">></span></span>{{ count * 10 }}<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>div</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<p>While this is extremely useful, they are limited to single expressions. And when they tend to get long, the code gets bloated and can be tough to read. And in case you need to repeat the same at multiple places, it leads to repetitive code.</p>\n<p>In comes the <code>computed</code> property to save the day. Let’s look at an example.</p>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-html line-numbers\"><code class=\"language-html\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>div</span> <span class=\"token attr-name\">id</span><span class=\"token attr-value\"><span class=\"token punctuation\">=</span><span class=\"token punctuation\">\"</span>app<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>{{ getFullName }}<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>div</span><span class=\"token punctuation\">></span></span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span></span></pre></div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">let</span> app <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Vue</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span>\n  el<span class=\"token punctuation\">:</span> <span class=\"token string\">'#app'</span><span class=\"token punctuation\">,</span>\n  data<span class=\"token punctuation\">:</span> <span class=\"token punctuation\">{</span>\n    firstName<span class=\"token punctuation\">:</span> <span class=\"token string\">'Light'</span><span class=\"token punctuation\">,</span>\n    lastName<span class=\"token punctuation\">:</span> <span class=\"token string\">'Yagami'</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  computed<span class=\"token punctuation\">:</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token function-variable function\">getFullName</span><span class=\"token punctuation\">:</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token keyword\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>firstName <span class=\"token operator\">+</span> <span class=\"token string\">' '</span> <span class=\"token operator\">+</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span>lastName<span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>And we get the full name printed! Now whenever you change the value of the data properties being used inside the <code>getFullName</code> computed property, it returns a new full name. So if you go ahead and make a change like <code>app.lastName = “Kira”</code>, a new full name ‘Light Kira’ gets printed!</p>\n<h2>Computed vs Methods</h2>\n<p>You must have noticed we could’ve received the same result by using methods.</p>\n<p>To begin with, although computed properties are functions at the end of the day, we didn’t have to call them by <code>getFullName()</code>, like methods. We can call them the same way we do with data properties.</p>\n<p>Although the end results are the same, the way computed and method properties get there are different. <strong>Computed properties are cached</strong>. What it means is that the result of a computed property is internally cached and is re-evaluated only when a reactive source gets updated. This means as long as <code>firstName</code> and <code>lastName</code> remains the same, subsequent calls to <code>getFullName</code> does not cause a re-execution of the function and in turn returns the previously calculated cached result.</p>\n<p>It is also important to note that the dependant property has to be reactive. Hence the following will not cause a re-render.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\">computed<span class=\"token punctuation\">:</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token function-variable function\">nowDate</span><span class=\"token punctuation\">:</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> Date<span class=\"token punctuation\">.</span><span class=\"token function\">now</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>It will render once and will not update on subsequent re-renders.</p>\n<p>A huge upside of using computed over methods is the caching. In a case where we have a huge computed property with a lot of computations, execution of computed property on every re-render even when the data source hasn’t changed can be expensive. Computed property lets us skip this by caching the result and returning the same till a re-execution is needed.</p>\n<p>But that’s not all! In the next post, we have another property to look out for. Or should I say, watch out for?</p>\n<p>Until next time, bye!</p>","frontmatter":{"title":"Computed Properties, What Are They?","date":"August 21, 2019","description":"While methods are useful, we have an alternative that can be more efficient. Computed Properties."}}},"pageContext":{"isCreatedByStatefulCreatePages":false,"slug":"/computed-properties-what-are-they/","previous":{"fields":{"slug":"/methods-in-vue.js/"},"frontmatter":{"title":"Methods in Vue.js"}},"next":{"fields":{"slug":"/watchers-to-watch-your-properties/"},"frontmatter":{"title":"Watchers, to Watch Your Properties!"}}}}}