Is Vuex capable of caching watched properties similar to the way Vue's computed properties do?

The Vue documentation highlights how computed properties are intelligently cached rather than running the function on every re-render, as stated here:

On the other hand, invoking a method will always execute the function whenever there is a re-render. The necessity of caching becomes evident when dealing with expensive computed properties...

Here's my query: Do Vuex watched properties also make use of caching similar to computed properties? (including Vuex watching, for instance using vm.$store.watch...)

Answer №1

Although Saurabh's response is technically accurate, I believe it misses the mark in addressing the initial query.

In reality, watchers are not "cached" - this concept simply doesn't align with the nature of watchers. Watchers are essentially functions that trigger side effects without returning values or being used as properties.

Therefore, there is no need or logic in caching information for watchers.

However, both functionalities only come into effect when the monitored data undergoes changes.

Answer №2

The operation of observers will mimic that of calculated since calculated is essentially built on top of observers. Whenever a calculated attribute is defined, Vue automatically assigns an observer to the variables utilized within the calculated property. The following code snippet can be found in the source:

function createCalculatedGetter (getter: Function, owner: Component): Function {
  const observer = new Observer(owner, getter, noop, {
    lazy: true
  })
  return function calculatedGetter () {
    if (observer.dirty) {
      observer.evaluate()
    }
    if (Dep.target) {
      observer.depend()
    }
    return observer.value
  }
}

Therefore, any code written within calculated or observe sections will only run once, triggered by changes in the reactive data.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Verify if the element is present before commencing any tasks

Looking for a solution: <div class="checkout-related-view__related-row"> <div class="checkout-related-view__related-row-cell checkout-related-view__related-row-cell--left"> <input type="checkbox" class="checkbox related-checkbox" id ...

I'm interested in learning more about how to select or deselect all checkboxes

How can I uncheck the "all" checkbox when I uncheck another checkbox? $('#All').click(function () { var status = $(this).is(":checked"); if (status) { $.each($('input[name="checkbox"]'), function () { this. ...

How can one effectively track changes in authentication state within AWS Cognito?

In my React app, I have integrated AWS Cognito for handling authentication. Rather than utilizing the components offered by the AWS React package, I have opted to work with APIs in the AWS JavaScript library. My goal is to monitor changes in the authentica ...

Having trouble with the post request in Express JS and Vue? Don't worry, we've got you covered

I've been following a tutorial to set up a basic app and everything is working smoothly, except for the post request which seems to be causing me trouble. Tutorial: https://www.youtube.com/watch?v=HkIGAqAP1GY Although the issue is reproducible based ...

What is the best way to input sorted data from a variable into a textarea field?

HTML: <button id="myButton">Click Here</button> <br /> <textarea id="imgLinks" rows="10" cols="30"></textarea> JS: $(document).ready(function() { $("#myButton").click(function() { var finalLink = '["img/1.j ...

I noticed that after updating my notification count, there is an unexpected gap in the box. How can I go about fixing this issue?

Would it be helpful to include an image to illustrate my point? I am trying to remove the extra space on the left of the numbers that appear after clicking on a notification to mark it as read and update the notification count. Below is the code snippet I ...

What is the best way to iterate through these arrays to retrieve just the orderTotal values?

How can I iterate through this array and calculate the total order amount within each Item object? (0) OrderTotal $100 (1) OrderTotal $220 var sum = 320; store the combined OrderTotal values in a variable I aim to obtain the total sum of all order tot ...

Is there a way for me to send a form containing pre-existing data from a different page?

Seeking advice: I realize that utilizing jQuery's ajax function may be the simplest approach, however I am facing an issue with the form field names. The var_dump below displays the typical values submitted by the form using test data. It appears that ...

Using Ajax without implementing JavaScript

Is it possible to develop an application that utilizes Ajax without relying on JavaScript, allowing it to function even if JavaScript is disabled by the user in their browser? Are there any restrictions or limitations to consider? ...

Locate all the properties that are empty within each object contained in a JavaScript array

Consider this scenario: if I were to have an array of JavaScript objects like the following: var jsObjects = [ {a: 1, b: 2, c: null, d: 3, e: null}, {a: 3, b: null, c: null, d: 5, e: null}, {a: null, b: 6, c: null, d: 3, e: null}, {a: null, ...

Creating new instances of objects in Express.js with routers

Many examples show authors using classes with static methods and passing those classes with static methods to the router. router.post('/user/name', (req, res, next) => { UserController.getName(req, res, next); }); router.post('/use ...

Reverse action on Angular checklist-model checkboxes

I have successfully implemented checklist-model.js for angular to select from a dynamically generated list of objects. However, I now need to create the functionality to move unchecked checkboxes into a new array (and remove them when checked again). Can a ...

Issue with Cordova file plugin's inability to write to file

Currently tackling an issue with the Cordova file plugin (version 3.4.1) on Android. Despite combing through documentation, including the HTML5 file storage API and numerous StackOverflow posts (most of which suggest installing the plugin first - already d ...

What is the best way to retrieve a document from a collection in a Meteor application?

My experience with mongodb is very limited, so I need help on how to retrieve a document from a meteor collection. I am trying to check if a document exists for the user and update it with an object. if (Saves.find({_id: Meteor.userId()}).fetc ...

PHP header not working in AJAX redirect

As part of a project, I am tasked with creating a basic log in system using REST to manage the entire page. However, despite my efforts, I seem to be missing or misunderstanding some key concepts that are making it difficult for me to progress. One restric ...

I am interested in updating the content on the page seamlessly using Angular 6 without the need to reload

As a newcomer to Angular, I am interested in dynamically changing the page content or displaying a new component with fresh information. My website currently features cards, which you can view by following this Cards link. I would like to update the page ...

Ensuring the validity of float and non-empty values in JavaScript

Embarking on the journey of web development, I'm delving into basic applications. My current project involves creating a straightforward webpage to add two numbers and implementing preliminary validations - ensuring that the input fields are not left ...

What is the best method for passing a JavaScript object to PHP using Ajax?

I have looked into similar questions like this and this, but none of them have helped me solve my issue. When I check the console log for my data, it displays the following: Object["row_LM#00000010", "row_LM#00000002", "row_LM#00000009", "row_LM#00000008" ...

Finding the exact x and y coordinates of an object within the camera's frame

I'm currently working on figuring out the exact location (x, y, z coordinates) of an object (a BoxGeometry) in the world in reference to the screen (frustum) rather than the world itself. While the frustum.intersectsObject(…) method provides a Boole ...

Vue component not displaying data from Axios response variable on browser

Vue component variable is being set with data from Axios response, however the data is not displaying on the browser. Although the data is visible in the console, it doesn't show up on the Browser. The data is formatted in JSON. Snapshot: Output: ...