#Performance question

1 messages · Page 1 of 1 (latest)

raw patio
#

Why does my program measure an increased speed of an asynchronous function when it is minimized, and steadily decrease in measured when the window is open?

async function fetchLiveData(event, id){
  const startTime = performance.now();

  const dataQuery = ' SELECT * FROM "table" WHERE id = $1 ';
  const result = await client.query(dataQuery , [id]);

  const endTime = performance.now();
  console.log(`fetchLiveData took ${endTime - startTime} milliseconds to complete.`);
}

While the window is minimized, the measured time drops to 300ms and then stops updating the console
Then when the window is open, the measured time increases steadily, which does not reflect what is actually happening. This fetchLiveData function is on a 250ms timer, and it is executing fine

sonic shore
#

its question to await client.query

raw patio
#

Well, I am using https://www.npmjs.com/package/pg
It says it supports async

#

But i don't think that explains the variation in speed if the window is open or minimized

sonic shore
#

window in background throttle timers

#

how that module work big question

raw patio
#

Wow yeah I can not have windows throttling my timer

#

So, my query was taking about 500ms to return, which is faster than the 250ms timer that the function gets called on

#

I set the timer to 1000ms and it runs constantly at 300-400ms

sonic shore
#

it doesnt explain 5sec to complete query

raw patio
#

it does, i think

sonic shore
#

no

#

you output inner code block execution timer

raw patio
#

calling the function more than once while its still waiting for a promise

sonic shore
#

it doesnt show how setTimer works

raw patio
#

renderer.js:

dataUpdateInterval = setInterval(async () => {
  await window.electronAPI.handleLiveData(Number(document.getElementById('id').value)).then(async newLiveData => await testFunc(newLiveData));
    }, 1000);

preload.js:
handleLiveData: (id) => ipcRenderer.invoke('handle-live-data', id),

main.js:
ipcMain.handle('handle-live-data', fetchLiveData);

database module:

  try {
    const startTime = performance.now();

    const latestDataQuery = 'SELECT * FROM table WHERE id = $1 ';
    const result = await client.query(latestDataQuery, [id]);

    const endTime = performance.now();
    console.log(`fetchLiveData took ${endTime - startTime} milliseconds to complete.`);

    return result;
    
  } catch (err) {
    console.error('Error fetching data:', err);
  }
}
sonic shore
#

interval doesnt work with async/await

#

it doesnt wait function to complete

raw patio
#

That would certainly explain things

sonic shore
#

and in background it may execute every 60 sec for example

raw patio
#

That is exactly what I am seeing, yes

sonic shore
#

strange decision to use pg with electron

raw patio
#

maybe, but i have a data aggregation program that stores gb's worth of relational tables

sonic shore
#

another 3rd party library?

raw patio
sonic shore
#

you can try to use your own implementation

#

such basic stuff shouldnt be a problem

#

or use while loop for example

raw patio
#

welp, that would probably make way more sense lol

#

and hopefully wont get throttled

sonic shore
#

depends on how you impletent it

#

making query every 200ms not that good thing

#

*without throttling

raw patio
#

yeah, i need to index the columns in the database first