<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Thorsten Lünborg</title>
    <description>The latest articles on DEV Community by Thorsten Lünborg (@linusborg).</description>
    <link>https://dev.to/linusborg</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F142972%2Fae53eaa9-cd7d-4dfa-8dad-274112ffd323.jpeg</url>
      <title>DEV Community: Thorsten Lünborg</title>
      <link>https://dev.to/linusborg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/linusborg"/>
    <language>en</language>
    <item>
      <title>Vue: When a computed property can be the wrong tool</title>
      <dc:creator>Thorsten Lünborg</dc:creator>
      <pubDate>Sun, 05 Sep 2021 12:57:45 +0000</pubDate>
      <link>https://dev.to/linusborg/vue-when-a-computed-property-can-be-the-wrong-tool-195j</link>
      <guid>https://dev.to/linusborg/vue-when-a-computed-property-can-be-the-wrong-tool-195j</guid>
      <description>&lt;p&gt;If you're a Vue user, you likely know computed properties, and if you are like me, you probably think they are awesome - rightfully so!&lt;/p&gt;

&lt;p&gt;To me, computed properties are a very ergonomic and elegant way to deal with derived state - that is: state which is made up from other state (its &lt;em&gt;dependencies&lt;/em&gt;). But in some scenarios, they can also have a &lt;em&gt;degrading&lt;/em&gt; effect on your performance, and I realized that many people are unaware of that, so this is what this article will attempt to explain.&lt;/p&gt;

&lt;p&gt;To make clear what we are talking about when we say "computed properties" in Vue, here's a quick example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;reactive&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Wahs Dishes&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Throw out trash&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;openTodos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hasOpenTodos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!!&lt;/span&gt;&lt;span class="nx"&gt;openTodos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;openTodos&lt;/code&gt; is derived from &lt;code&gt;todos&lt;/code&gt;, and &lt;code&gt;hasOpenTodos&lt;/code&gt; is derived from &lt;code&gt;openTodos&lt;/code&gt;. This is nice because now we have reactive objects that we can pass around and use, and they will automatically update whenever the state that they depend on, changes.&lt;/p&gt;

&lt;p&gt;If we use these reactive objects in a reactive context, such as a Vue template, a render function or a &lt;code&gt;watch()&lt;/code&gt;,  these will also react to the changes of our computed property and update - that's the magic at the core of Vue that we value so much, after all.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: I'm using composition API because that's what I like to use these days. The behaviors describes in this article apply to computed properties in the normal Options API just as much, though. Both use the same reactivity system, after all.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is special about computed properties
&lt;/h2&gt;

&lt;p&gt;There's two things about computed properties that make them special and they are relevant to the point of this article:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Their results are cached and only need to be re-evaluated once one of its reactive dependencies changes.&lt;/li&gt;
&lt;li&gt;They are evaluated lazily on access.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Caching
&lt;/h3&gt;

&lt;p&gt;A computed property's result is cached. In our example above, that means that as long as the &lt;code&gt;todos&lt;/code&gt; array doesn't change, calling &lt;code&gt;openTodos.value&lt;/code&gt; multiple times will return the same value &lt;em&gt;without re-running the filter method&lt;/em&gt;. This is especially great for expensive tasks, as this ensures that the task is only ever re-run when it has to – namely when one of its reactive dependencies has changed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lazy Evaluation
&lt;/h3&gt;

&lt;p&gt;Computed properties are also evaluated &lt;em&gt;lazily&lt;/em&gt; – but what does that mean, exactly? &lt;/p&gt;

&lt;p&gt;It means that the callback function of the computed property will only be run once the computed's value is being read (initially or after it was marked for an update because one of its dependencies changed). &lt;/p&gt;

&lt;p&gt;So if a computed property with an expensive computation isn't used by anything, that expensive operation won't even be done in the first place - another performance benefit when doing heavy lifting on a lot of data. &lt;/p&gt;

&lt;h2&gt;
  
  
  When lazy evaluation can &lt;em&gt;improve&lt;/em&gt; performance
&lt;/h2&gt;

&lt;p&gt;As explained in the previous paragraph, lazy evaluation of computed properties is a usually a good thing, especially for expensive operations: It ensures that the evaluation is only ever done when the result is actually needed.&lt;/p&gt;

&lt;p&gt;This means that things like filtering a big list will simply be skipped if that filtered result won't be read and used by any part of your code at that moment. Here's a quick example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;template&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;v-model=&lt;/span&gt;&lt;span class="s"&gt;"newTodo"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"button"&lt;/span&gt; &lt;span class="na"&gt;v-on:click=&lt;/span&gt;&lt;span class="s"&gt;"addTodo"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Save&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;click=&lt;/span&gt;&lt;span class="s"&gt;"showList = !showList"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    Toggle ListView
  &lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;template&lt;/span&gt; &lt;span class="na"&gt;v-if=&lt;/span&gt;&lt;span class="s"&gt;"showList"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;template&lt;/span&gt; &lt;span class="na"&gt;v-if=&lt;/span&gt;&lt;span class="s"&gt;"hasOpenTodos"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;{{ openTodos.length }} Todos:&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt; 
      &lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;v-for=&lt;/span&gt;&lt;span class="s"&gt;"todo in openTodos"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
          {{ todo.title }}
        &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;v-else&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;No todos yet. Add one!&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;setup&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;showListView&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;reactive&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Wahs Dishes&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Throw out trash&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;openTodos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;hasOpenTodos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!!&lt;/span&gt;&lt;span class="nx"&gt;openTodos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newTodo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;addTodo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;todos&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;See This code running on the &lt;a href="https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdCBzZXR1cD5cbmltcG9ydCB7IHJlYWN0aXZlLCByZWYsIGNvbXB1dGVkIH0gZnJvbSAndnVlJ1xuXG5jb25zdCBzaG93TGlzdCA9IHJlZihmYWxzZSlcbiAgXG5jb25zdCB0b2RvcyA9IHJlYWN0aXZlKFtcbiAgeyB0aXRsZTogJ1dhaHMgRGlzaGVzJywgZG9uZTogdHJ1ZX0sXG4gIHsgdGl0bGU6ICdUaHJvdyBvdX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wYW4+XG4gIDwvdGVtcGxhdGU+XG4gIFxuPC90ZW1wbGF0ZT4ifQ=="&gt;SFC Playground&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;showList&lt;/code&gt; is initially &lt;code&gt;false&lt;/code&gt;, the template/render function will not read &lt;code&gt;openTodos&lt;/code&gt;, and consequently, the filtering would not even happen, neither initially nor after a new todo has been added and &lt;code&gt;todos.length&lt;/code&gt; has changed. Only after &lt;code&gt;showList&lt;/code&gt; has been set to &lt;code&gt;true&lt;/code&gt;, these computed properties would be read and that would trigger their evaluation.&lt;/p&gt;

&lt;p&gt;Of course in this small example, the amount of work for filtering is minimal, but you can imagine that for more expensive operations, this can be a huge benefit.&lt;/p&gt;

&lt;h2&gt;
  
  
  When lazy evaluation can &lt;em&gt;degrade&lt;/em&gt; performance
&lt;/h2&gt;

&lt;p&gt;There is a downside to this: If the result returned by a computed property can only be known after your code makes use of it somewhere, that also means that Vue's Reactivity system can't know this return value beforehand.&lt;/p&gt;

&lt;p&gt;Put another way, Vue can realize that one or more of the computed property's dependencies have changed and so it should be re-evaluated the next time it is being read, but Vue can't know, at that moment, wether the &lt;em&gt;result&lt;/em&gt; returned by the computed property would actually be different.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why can this be a problem?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Other parts of your code may depend on that computed property – could be another computed property, could be a &lt;code&gt;watch()&lt;/code&gt;, could be the template/render function. &lt;/p&gt;

&lt;p&gt;So Vue has no choice but to mark these dependents for an update as well – "just in case" the return value will be different. &lt;/p&gt;

&lt;p&gt;If those are expensive operations, you might have triggered an expensive re-evaluation even though your computed property returns the same value as before, and so the re-evaluation would have been unnecessary.&lt;/p&gt;

&lt;h3&gt;
  
  
  Demonstrating the issue
&lt;/h3&gt;

&lt;p&gt;Here's a quick example: Imagine we have a list of items, and a button to increase a counter. Once the counter reaches 100, we want to show the list in reverse order (yes, this example is silly. Deal with it).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(You can play with this example on this &lt;a href="https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdCBzZXR1cD5cbiAgaW1wb3J0IHsgcmVmLCByZWFjdGl2ZSwgY29tcHV0ZWQsIG9uVXBkYXRlZCB9IGZyb20gJ3Z1ZSdcblxuICBjb25zdCBsaXN0ID0gcmVhY3RpdmUoWzEsMiwzLDQsNV0pXG4gIFxuICBjb25zdCBjb3VudCA9IHJlZigwKVxuICBmdW5jdGlvbiBpbmNyZWFzZSgpIHtcbiAgICBjb3VudC52YWx1ZSsrXG4gIH1cbiAgXG4gIGNvbnN0IGlzT3ZlcjEwMCA9IGNvbXB1dGVkKCgpID0+IGNvdW50LnZhbHVlID4gMTAwKVxuICBcbiAgY29uc3Qgc29ydGVkTGlzdCA9IGNvbXB1dGVkKCgpID0+IHtcbiAgICAvLyBpbWFnaW5lIHRoaXMgdG8gYmUgZXhwZW5zaXZlXG4gICAgcmV0dXJuIGlzT3ZlcjEwMC52YWx1ZSA/IFsuLi5saXN0XS5yZXZlcnNlKCkgOiBbLi4ubGlzdF1cbiAgfSlcbiAgXG4gIG9uVXBkYXRlZCgoKSA9PiB7XG4gICAgY29uc29sZS5sb2coJ2NvbXBvbmVudCByZS1yZW5kZXJlZCEnKVxuICB9KVxuICBcbjwvc2NyaXB0PlxuXG48dGVtcGxhdGU+XG4gIDxidXR0b24gQGNsaWNrPVwiaW5jcmVhc2VcIj5cbiAgICBDbGljayBtZVxuICA8L2J1dHRvbj5cbiAgPGJyPlxuICA8aDM+XG4gICAgTGlzdFxuICA8L2gzPlxuICA8dWw+XG4gICAgPGxpIHYtZm9yPVwiaXRlbSBpbiBzb3J0ZWRMaXN0XCI+XG4gICAgICB7eyBpdGVtIH19XG4gICAgPC9saT5cbiAgPC91bD5cbjwvdGVtcGxhdGU+In0="&gt;SFC playground&lt;/a&gt;)&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;template&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="na"&gt;click=&lt;/span&gt;&lt;span class="s"&gt;"increase"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    Click me
  &lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;br&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;h3&amp;gt;&lt;/span&gt;
    List
  &lt;span class="nt"&gt;&amp;lt;/h3&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;v-for=&lt;/span&gt;&lt;span class="s"&gt;"item in sortedList"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      {{ item }}
    &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/template&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;setup&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reactive&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onUpdated&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;reactive&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;increase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isOver100&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sortedList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// imagine this to be expensive&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;isOver100&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;onUpdated&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// this eill log whenever the component re-renders&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;component re-rendered!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Question&lt;/strong&gt;: You click the button 101 times. How often does our component re-render?&lt;/p&gt;

&lt;p&gt;Got your answer? You sure?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt; It will re-render &lt;strong&gt;101 times*.&lt;/strong&gt;*&lt;/p&gt;

&lt;p&gt;I suspect some of you might have expected a different answer, something like: "once, on the 101st click". But that's wrong, and the reason for this is the lazy evaluation of computed properties.&lt;/p&gt;

&lt;p&gt;Confused? We'll walk through what's happening step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When we click the button, the &lt;code&gt;count&lt;/code&gt; is increased. The component would not re-render, because we don't use the counter in the template.&lt;/li&gt;
&lt;li&gt;But since &lt;code&gt;count&lt;/code&gt; changed, our computed property &lt;code&gt;isOver100&lt;/code&gt;is marked as "dirty" - a reactive dependency changed, and so its return value has to be re-evaluated.&lt;/li&gt;
&lt;li&gt;But due to lazy evaluation, that will only happen once something else reads &lt;code&gt;isOver100.value&lt;/code&gt; - before that happens, we (and Vue) don't know if this computed property will still return &lt;code&gt;false&lt;/code&gt; or will change to &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sortedList&lt;/code&gt;depends on &lt;code&gt;isOver100&lt;/code&gt; though - so it also has to be marked dirty. And likewise, it won't yet be re-evaluated because that only happens when it's being read.&lt;/li&gt;
&lt;li&gt;Since our template depends on &lt;code&gt;sortedList&lt;/code&gt;, and it's marked as "dirty" (potentially changed, needs re-evaluation), the component re-renders.&lt;/li&gt;
&lt;li&gt;During rendering, it reads &lt;code&gt;sortedList.value&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sortedList&lt;/code&gt; now re-evaluates, and reads &lt;code&gt;isOver100.value&lt;/code&gt; – which now re-evaluates, but still returns &lt;code&gt;false&lt;/code&gt; again.&lt;/li&gt;
&lt;li&gt;So now we have re-rendered the component &lt;em&gt;and&lt;/em&gt; re-run the "expensive" &lt;code&gt;sorteList&lt;/code&gt;computed even though all of that was unnecessary - the resulting new virtual DOM / template will look exactly the same.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The real culprit is &lt;code&gt;isOver100&lt;/code&gt; – it is a computed that often updates, but usually returns the same value as before, and on top of that, it's a cheap operation that doesn't really profit from a the caching computed properties provide. We just used a computed because it feels ergonomic, it's "nice".&lt;/p&gt;

&lt;p&gt;When used in another, expensive computed (which &lt;em&gt;does&lt;/em&gt; profit from caching) or the template, it will trigger unnecessary updates that can seriously degrade your code's performance depending on the scenario.&lt;/p&gt;

&lt;p&gt;It's essentially this combination:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;An expensive computed property, watcher or the template depends on&lt;/li&gt;
&lt;li&gt;another computed property that often re-evaluates to the same value.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How to solve this problem when you come across it.
&lt;/h2&gt;

&lt;p&gt;By now you might have two questions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Wow! Is this a bad problem?&lt;/li&gt;
&lt;li&gt;How do I get rid of it?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So first off: &lt;strong&gt;Chill&lt;/strong&gt;. Usually, this is &lt;strong&gt;not a big problem&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Vue's Reactivity System is generally very efficient, and re-renders are as well, especially now in Vue 3. usually, a couple unnecessary updates here and there will still perform much better than, say, a React counterpart that by default, re-renders on &lt;em&gt;any state change whatsoever&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So the problem only applies to specific scenarios where you have a mix of frequent state updates in one place, that trigger frequent unnecessary updates in another place that is expensive (very large component, computationally heavy computed property etc).&lt;/p&gt;

&lt;p&gt;If you encounter such a situation, you can optimize it with a custom little helper:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;&lt;br&gt;
In a previous version of this article, I orginally had mentioned 3 points here. Two of them were, in the end, not really relevant to the problem but rather remnants of a previous draft I erroneously shoehorned into this.&lt;br&gt;
I removed those for the sake of clarity and getting to the point.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Custom &lt;code&gt;eagerComputed&lt;/code&gt; helper
&lt;/h3&gt;

&lt;p&gt;Vue's Reactivity System gives us all of the required tools to build our own version of a &lt;code&gt;computed()&lt;/code&gt;, one that evaluates &lt;em&gt;eagerly&lt;/em&gt;, not &lt;em&gt;lazily&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Let's call it &lt;code&gt;eagerComputed()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;watchEffect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;shallowRef&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;readonly&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;eagerComputed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;shallowRef&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;watchEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; 
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;flush&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sync&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;// needed so updates are immediate.&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;readonly&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can then use this like we would use a computed property, but the difference in behavior is that the update will be eager, not lazy, getting rid of unnecessary updates.&lt;/p&gt;

&lt;p&gt;Check out the fixed example on &lt;a href="https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdCBzZXR1cD5cbiAgaW1wb3J0IHsgcmVmLCByZWFjdGl2ZSwgc2hh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DxoMz5cbiAgICBMaXN0XG4gIDwvaDM+XG4gIDx1bD5cbiAgICA8bGkgdi1mb3I9XCJpdGVtIGluIHNvcnRlZExpc3RcIj5cbiAgICAgIHt7IGl0ZW0gfX1cbiAgICA8L2xpPlxuICA8L3VsPlxuPC90ZW1wbGF0ZT4ifQ=="&gt;this SFC Playground&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When would you use &lt;code&gt;computed()&lt;/code&gt; and when &lt;code&gt;eagerComputed()&lt;/code&gt;?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;computed()&lt;/code&gt;when you have a complex calculation going on, which can actually profit from caching and lazy evaluation and should only be (re-)calculated if really necessary.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;eagerComputed()&lt;/code&gt; when you have a simple operation, with a rarely changing return value – often a boolean.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Keep in mind that this helper uses a sync watcher, which means it will evaluate on each reactive change synchronously and individually - if a reactive dependency changes 3 times, this will re-run 3 times. So it should only be used for &lt;em&gt;simple and cheap&lt;/em&gt; operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finishing up
&lt;/h2&gt;

&lt;p&gt;So this is it. We dove deeper into how computed properties actually work. We learned when they are beneficial for your app's performance, and when they can degrade it. Concerning the latter scenario, we learned how to solve the performance problem by avoiding unnecessary reactive updates with an eagerly evaluating helper.&lt;/p&gt;

&lt;p&gt;I hope this was helpful. Let me know if you have questions, and tell me other topics you may want me to cover.&lt;/p&gt;

</description>
      <category>vue</category>
      <category>frontend</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
