Improving Performance in Vue by Reducing `$set` Usage

Sharing my code snippet below


    <div v-for="namespace in chosenNamespaces" v-bind:key="namespace.id">

        <!-- Select the Parameter -->

        <select @change="updateParameter($event, namespace.id)" v-model="currParameterValues[namespace.id]">
            <option v-for="parameter in getParametersForNamespace(namespace.id)">{{parameter}}</option>
        </select>

        <!-- Select Property -->

        <select @change="updatePropertyType($event, namespace.id)" v-model="currPropertyValues[namespace.id]">
            <option v-for="property in getPropertiesForNamespace(namespace.id)">{{property}}</option>
        </select>

        <!-- Select Item -->
        <select v-model="currItemValues[namespace.id]">
            <option v-for="item in getItemsForNamespace(namespace.id)">{{item}}</option>
        </select>
    </div>


methods: {
    updateParameter(data, id){
        ....
        this.$set(currParameterValues, id, data,target.value)
        this.$set(currPropertyValues, id, someValue)
    }

    updatePropertyType(data, id){
        ...
        this.$set(currPropertyValues, someThing, val)
    }
}

I have multiple div elements that iterate through the list of chosenNamespaces array and generate a group of selection fields. What I am trying to achieve is to update the value of the second select field (Select for Property) when I change the value of the corresponding parameter select field (Select paramater) for that particular namespace using the updateParameter method. To do this, I use $set to modify the currPropertyValues array. However, I have noticed that whenever I change the parameter option, there is a delay of around 4-5 seconds before it processes the change. This delay might be due to Vue taking time to react to the modification in the property array values. If I remove $set from updateParameter, the response is immediate. How can I resolve this issue?

Edit: I have recreated the scenario on a fiddle where changing a dropdown value has a delay in reflecting the change: fiddle

Answer №1

The reason this occurs is due to the usage of array indexes as object keys using v-model, resulting in the creation of extremely large arrays. For instance, when executing the following code snippet, a massive array with 152,395,893 items is generated, leading to sluggish performance within a template:

const arr = [];
arr[152395893] = '...';

In an array context, the number serves as a numerical index rather than a key name. What you require is an object, which only creates a single item:

const obj = {};
obj[152395893] = '...';

To resolve this issue, convert both to objects:

currParameterValues: {},
currOperatorValues: {}

View the modified Fiddle here

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

Identify the Presence of Hover Functionality

For a while now, the trend has been leaning towards feature detection. I am interested in determining whether a visitor's browser supports the :hover pseudo class. With many mobile devices not supporting hovering, I want to adjust my event listeners a ...

React Native: Issue with Higher Order Component when utilizing the `onLayout` prop

The functionality of this component involves rendering the Placeholder. Once it acquires its layout, this higher-order component (HOC) updates the state and invokes the child component with the values. Everything seems to be working fine up to this point. ...

Error Alert: VueRouter has not been properly defined

I am completely new to fullstack development and was recommended to check out this tutorial on creating WedAPI and VueJS. Prior to this, I have only worked with Python, PERL, and C#. The tutorial can be found at: https://www.youtube.com/watch?v=qS833HGKPD8 ...

Issues with the functionality of the map method in Javascript arrays

I have integrated Laravel and Vue.js to develop a chat application. To secure my response in Laravel, I implemented encryption to prevent unauthorized access from the console: public function get() { $contacts = User::where('id', '!=&ap ...

Refreshing issue: Model change in child page not updating view correctly (Ionic & Angular)

I am currently working with Ionic 3.20 and Angular 5.2.9, encountering an issue with content refreshing after a model change. Despite being new to this, I sense that I might be overlooking something fundamental. Within my view, I have the following elemen ...

Using the map function with a React onClick handler

After tirelessly scouring the internet for answers to my query, I've hit a dead end. My goal is to implement a basic react click handler on my button, but for some reason, it just won't cooperate. It's likely something incredibly straightfor ...

What is the best way to dynamically select and deselect radio buttons based on a list of values provided by the controller?

I am currently working on implementing an edit functionality. When the user initially selects a list of radio buttons during the create process, I store them in a table as a CSV string. Now, during the edit functionality, I retrieve this list as a CSV stri ...

Automatically calculate line total using jQuery when input blurs

Within my interface, I have a section that allows users to input their class information for reimbursement. There are 6 line items available for them to fill out with the cost of books and tuition. Ideally, I would like the user to be able to enter these c ...

What is the best way to extract a nested array of objects and merge them into the main array?

I've been working on a feature that involves grouping and ungrouping items. A few days ago, I posted this question: How can I group specific items within an object in the same array and delete them from the core array? where some helpful individuals ...

Conceal one element and reveal a different one upon submitting a form

On a web page, I want to hide the form upon submission (either by clicking or pressing enter) and display the results. However, this functionality does not seem to work when the Go web server is running. When I test the HTML file (without executing the Go ...

How can I transfer a variable from a Symfony server to a VITE VUE server successfully resolved

Here is the code for view01.vue <script setup lang = 'ts'> import { ref } from 'vue' const message = ref('') fetch("/api") .then(response => response.json()) .then(data => { message.value = JSON.stringif ...

Transform the file format from .casa-model to .obj

Looking for a way to convert a file with the extension .casa-model to .obj format. Is there any solution available? Find the model here. ...

Effortlessly download multiple mp4 files simultaneously using jQuery through the console

When I open HTML pages in a new window, a media file ".mp4" is among the elements. To save only the media content within each page, I have this code: Is there a way to identify and download all external media files loaded on these pages? var anchor = docu ...

Preventing scrolling in one div while focusing on another div

I am currently in the process of creating a website that functions as a single page site. The main feature of the site is a masonry grid of images. When a user clicks on an item, a detailed panel slides in from the left. Once the panel is closed, it slide ...

False return - Inoperative link - Scroll option

I have a JavaScript drop-down menu that is functioning correctly, but when I click on one of the links in the dropdown, the link does not work. I suspect it has something to do with my "return false/true" setup. Here's my JavaScript code: function d ...

Using onChange input causes the component to re-render multiple times

As I delve into learning React, I've encountered some challenges. Despite thinking that I grasped the concept of controlled components, it appears that there's a misunderstanding on my end. The issue arises after an onChange event where I notice ...

Using two loops/arrays in JavaScript to generate a rating score

I want to create a simple rating component with the following appearance: ◼◼◼◻◻ (score: 3 out of 5) Here is my JSX code snippet: var score = 3; var range = 5; { [...Array(range)].map((e, i) => ( <div className="rating-item" ...

Using a series of identical divs to dynamically update the image URL

Greetings! I am a newcomer to the world of web development and I have decided to hone my skills by creating a small website for my mother! My goal is to replicate a specific div multiple times while changing only the image URL and the heading caption. < ...

Discover which npm module includes the lodash dependency

I've encountered a peculiar situation while using webpack to create a production bundle for my application. Even though I haven't explicitly installed `lodash` and it's not listed in my package.json file, I noticed that it's being added ...

Can Angular Universal SSR be activated specifically for Googlebot User Agents?

I am aiming to activate Angular Universal SSR only when the User Agent is identified as Googlebot. However, I am uncertain about how to instruct Angular Universal SSR to deliver server side rendered HTML exclusively if the User Agent is Googlebot. server ...