DEV Community

Cover image for Vue JS - Differences between Computed and Watch
Guillermo Verón
Guillermo Verón

Posted on • Updated on • Originally published at guivern.hashnode.dev

Vue JS - Differences between Computed and Watch

When I started to code in Vue and had to react to changes, I always asked myself the same question: computed or watch? 🤔️. The both seem to do the same, right? However, a computed property may be better than a watcher in some cases or vice versa.

Computed

We can think of a computed property as a normal property, but with the difference that Vue automatically evaluates its value every time one of its dependencies is changed. For instance:

computed: {
  total() {
    return this.price * this.quantity;
  }
}
Enter fullscreen mode Exit fullscreen mode

The total property will be calculated every time the price or the quantity changes.

Watch

A watcher is basically a function that Vue automatically executes when the watched property changes. If we take the above example, we could do it as follows:

watch: {
  quantity(val) {
    this.total = this.price * val;
  },
  price(val) {
    this.total = this.quantity * val;
  }
}

Enter fullscreen mode Exit fullscreen mode

This works, but it's a lot of code. We would have to add a third watcher if the total property also depended on the tax. This is the main reason why it's a better idea to use a computed property instead.

While computed properties are more appropriate in most cases, watchers are more powerful and allow us to perform complex operations such as HTTP calls or execute some asynchronous logic that wouldn't be possible with a computed property.

Suppose we want to get the movies in which an actor appears. We could do it in the following way:

export default {
  data() {
    return {
      actorId: null,
      movies: [],
    };
  },
  methods: {
    getMovies() {
      // http request
      axios.get(`movies/actor/${this.actorId}`)
      .then(resp => {
        this.movies = resp.data;
      });
    }
  },
  watch: {
    actorId(val) {
      if(val) this.getMovies();
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Another use case for a watcher is when we need to watch a data property until it reaches a specific value, and only then do something. For instance:

watch: {
  quantity(val) {
    if (val > this.stock) {
      this.error = true;
      this.errorMessage = `We only have ${this.stock} units`;
    }
  },
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Computed properties and watchers have different use cases. It would be a better idea to use a computed property whenever we need to calculate a value based on other properties, considering that Vue will reevaluate the property every time any of its dependencies is changed and we will not need a custom watcher for each dependency. But a watcher would be better if we need to do something more generic than just get a value, such as HTTP calls, store data in local storage, set a timer, or whatever we couldn't do with computed properties.

Top comments (2)

Collapse
 
frontendengineer profile image
Let's Code

They have different use cases.

Would use computed property to calculate one or more properties. One of the best advantages of this is caching.

Watch is expensive and should be used if one would want to trigger a side effect as a result of a property change just like calling an API like your example, additional logic.

Collapse
 
guivern profile image
Guillermo Verón • Edited

You've got it clear! That's the point.