Maintaining a reference list of HTML elements in a lengthy v-for loop can lead to performance issues

Task

I am facing a challenge with a long list of elements in a v-for loop. My goal is to trigger the .focus() method on the next element when the user presses the "arrow-down" key.

My Attempt

<script setup>
import { ref } from 'vue'
const htmlElements = ref([])
const elements = Array.from({ length: 5_000 }, (_, i) => ({ text: 'Element'+ i }))
</script>

<template>
 <p v-for="el of elements" ref="htmlElements">{{ el.text }}</p>
</template>

Vue Playground

I have a lengthy list of elements and I maintain a record of their corresponding DOM elements from the v-for loop for triggering focus (details not included for simplicity).

Issue

The use of ref="htmlElements" is causing a significant performance bottleneck. While directly accessing the DOM elements through the native childNodes property seems faster, it also appears fragile (involving filtering out unwanted #text nodes).

Query

How can I efficiently and Vue-compliantly invoke the focus function on the child nodes?

Answer №1

The original poster successfully improved performance by utilizing shallowRef. While not the most efficient solution, it serves as a temporary fix until a more substantial overhaul can be implemented.

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

Exploring the dynamic interaction of variables in OpenLayers and Thymeleaf using Javascript

My SpringBoot app is built using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine, and packaged as an executable JAR file. Within my Thymeleaf template, I am retrieving lat and lng variables from the controller. The following code snippe ...

JavaScript function encountering issues with AJAX integration

I have constructed the subsequent code to retrieve JSON data using a POST request. $.post("http://example.com/songs/search_api/index.php", "data[Song][keyword]=Stereophonics", function(data){ /*$("#results").append(data);*/ ...

`The challenge of properly handling quotation marks within quotation marks in JavaScript string literals

I am having trouble displaying text in a JavaScript tooltip Despite my efforts to escape the quotes and eliminate any line breaks, I keep encountering unterminated string literals. The specific text I want to show is: "No, we can't. This is going t ...

Navigating the cursor up and down seamlessly within the tags in Sublime Text

How can I navigate my cursor through code in Sublime Text on OS X using only the keyboard, so that it lands directly between opening and closing tags? For example, after creating a list with Emmet like ul>li*3, when I hit Tab, my cursor ends up between ...

A step-by-step guide on splitting an element into two separate elements using jquery

I have the following code snippet: <h4>user (role) on 26 Nov 2018 20:39:42 +00:00:</h4> My goal is to transform it into this structure: <h4>user (role)</h4> <p>on 26 Nov 2018 20:39:42 +00:00:</p> The <h4> eleme ...

Is it possible to combine Django urls and Vue routes in a single project?

After setting up my Django app and implementing the authentication layer using Django-Allauth with features like email confirmation, password reset, and two-factor authentication, I've come to the realization that for my app's interactive nature ...

Utilizing a drop-down menu to display two distinct sets of data tables

After spending some time on my WordPress site, I've encountered a problem that has me feeling stuck. Currently, I have two functioning datatables which are displaying one on top of the other on the page. Additionally, there is a dropdown selection box ...

Turn off the scrollbar without losing the ability to scroll

Struggling with disabling the HTML scrollbar while keeping the scrolling ability and preserving the scrollbar of a text area. Check out my code here I attempted to use this CSS: html {overflow:hidden;} Although it partially worked, I'm not complete ...

Selecting arbitrary elements from an array to generate a fresh array

I am working on a project where I need to select three random values from an array and display them in a new array on my website. It is important that each value is only selected once. Here is what I currently have: var students = ["Hans", "Ole", "Nils" ...

Show the JSON data that was returned

I'm encountering a problem trying to access and display the returned object. Since it's cross-domain, I'm using jsonp, but I'm unable to retrieve the returned object for some reason. $(function(){ var API = "https://ratesjson.fxcm. ...

Executing php class method through ajax with jQuery without requiring a php handler file

How can I trigger a PHP class method using AJAX with jQuery without the need for a separate PHP handler file? Here is my PHP Class animal.php:- <?php class animal { function getName() { return "lion"; } } ?> jQuery code snippet:- ...

What is the best method for uploading an image, video, or audio to Tumblr's API?

I've successfully shared quotes, texts, and links using Tumblr's API. However, I'm now curious about the process of posting images, videos, and audio through their API. While posting from a web URL seems straightforward, I am unsure of how t ...

Is there a way for me to retrieve the icons through a content query?

Currently, I am working on customizing a background slider. However, in the CSS file: span.cbp-binext:before { content: "\e000";} The issue I am facing is that I am only getting squares as icons. Is there a way for me to replace these squares with a ...

Vue.js is giving out a warning at line 465 stating that it has failed to produce the render function

I encountered an error that reads as follows: vue.js:465 [Vue warn]: Failed to generate render function: ReferenceError: Invalid left-hand side in assignment in with(this){return _c('div',{attrs:{"id":"test"}},[_c('p',[_v(_s(_f("sum ...

Is there an issue with Vue-router 2 where it changes the route but fails to update the view

I am currently facing an issue with the login functionality on a website that utilizes: Vue.js v2.0.3 vue-router v2.0.1 vuex v0.8.2 In routes.js, there is a basic interceptor setup router.beforeEach((to, from, next) => { if (to.matched.some(record ...

Discovering the result of the variable in a callback function within a Node

I am utilizing this specific NPM package to retrieve the CSV output of a table from an access database. The structure of my function is as follows: function createDatabase(mdbfile){ var rawDB = mdb(mdbfile); var db = {} rawDB.tables(function(err, ...

Does the onchange event differentiate between lowercase and uppercase letters?

Is the onchange event of the select tag case sensitive? Would it work if I use ONCHANGE instead of onchange? ...

Getting user information from VueJS using Axios is a common task that many developers need to tackle

Please provide guidance on what specific wording should be used in place of the following phrase? To retrieve user information from the database after logging in //ProfileDropDown.vue <template> <div class="text-right leading-tight hidden sm: ...

Ensuring the proper sequence of operations within a jQuery ajax callback function

I am facing an issue with my jQuery ajax function. The callback function includes two actions: 1) Taking the ajax result (which is an html form) and inserting it as the inner html of an html span. 2) Submitting this form How can I make sure that the form ...

Patience for AJAX call to complete in React and Redux

Is it necessary to display data only after the AJAX call has been completed? Check out my Reducer: import { INCOME_PROFILE } from '../actionTypes' import Immutable from 'immutable' const initialUserState = []; const profileReducer = ...