bfirsh / jsnes

A JavaScript NES emulator.
https://jsnes.org
Apache License 2.0
6.1k stars 823 forks source link

Improve frame loop performance by 15-20% #436

Open b4rtaz opened 1 year ago

b4rtaz commented 1 year ago

This commit increases the frame loop performance by 15-20%. How I tested it? I've turned on CPU throttling 6x in Chrome and I've run the below code.

const results = new Array(30);
let pos = 0;

function onAnimationFrame() {
   const start = Date.now();
   nes.frame();
   const end = Date.now();
   results[pos] = end - start;
   pos = (pos + 1) % results.length;
   if (pos === 0) {
      const avg = results.reduce((a, b) => a + b, 0) / results.length;
      console.log(avg);
   }
   // ...
}

This improvement is felt well on slow phones.

Results

Before my change:

Screenshot 2022-10-19 at 23 49 22

After my change:

Screenshot 2022-10-19 at 23 48 42