How do I speed up this process?

Hi,

I am still quite new to coding, and I have had a piece of code that queries different datasets based on a random number generated. The result will then be displayed on the page. What I have noticed is that there’s up to 3 secs delay when displaying results on the page. I am looking to speed up the display to less than 1 sec but i don’t know enough to make that happen. Can someone point a direction please?

Example of the code:

const getRandomNo = (max) =>{
return Math.floor(Math.random() * Math.floor(max));
}

let tip = getRandomNo(3)

if (tip === 0) {
queryInterests()
.then(result=>{
let items = result.items.sort(compare);
let index = getRandomNo(5);
let tipPercentage1 = items[index].percentage1;
let tipSubject = items[index].interests;
$w(‘#tipNumber’).text = ${tipPercentage1.toFixed(0)}%;
$w(‘#tipSubject’).text = ${tipSubject.toLowerCase()} ?;
})
}

Note 1: queryInterests() is a straight forward wix-data.query running at a backend file.
Note 2: percentage1 is generated through data hook afterQuery()

Does this process run on click? on load? on other occasions?

Hi J.D.

It runs when on load, when the page is ready.

If the user gets to this page from another page, then you can run this query in advance (when the user is on the previous page) and store the result to the memory.

Also I don’t know how big is your collection, but if it’s too big maybe you should select the single record on the backend.

Hi J.D.

They are not very big, but I can see you point about speeding up the process by being more precise and potentially pre-loading the data if it makes sense. Thanks for the perspectives, I will give these two directions a go and see which may be better. Cheers!