0

I have a simple code. The setInterval() should be running 60 times a second, yet console.log() shows only 35.

Also, this behavior happens on my client side. It appears whenever I use setInterval(), it runs at half the speed.

I've also tried the same with setTimeout(), which produces identical result.

Any ideas?

Node.js

const http          = require('http'),
      express       = require('express'),
      socketio      = require('socket.io'),
      process       = require('process'),
      config        = require('./config.js');

const settings = {
    cps: 60
};

var app = express();
var port = process.env.PORT || process.argv[2] || 8080;
var server = app.listen(port);
var io = socketio(server);

//Method 1
setInterval(() => {
    checkCPS();
}, 16.67);

//Method 2
//function update(){        
//  checkCPS();     
//  setTimeout(function(){
//      update();
//  }, 1000/settings.cps);
//}

var cps = 0,
    cpsCheck = 0,
    cpsSave = 0;
function checkCPS(){
    cps++;
    var d = new Date().getSeconds();
    if(cpsCheck != d){
        cpsCheck = d;
        cpsSave = cps;
        cps = 0;
        console.log(cpsSave);
    }
}

update();

Another test code, still getting 34 cycles only

const http          = require('http'),
      express       = require('express'),
      socketio      = require('socket.io'),
      process       = require('process'),
      config        = require('./config.js');

const settings = {
    cps: 60
};

var app = express();
var port = process.env.PORT || process.argv[2] || 8080;
var server = app.listen(port);
var io = socketio(server);
var User = {};
var SOCKET_LIST = {};
var PLAYER_LIST = {};

//Update LOBBIES
//setInterval(() => {
//  checkCPS();
//}, 16.67);

function update(){      
    setTimeout(function(){
        checkTime();
        update();
    }, 0);
}
var saveTime = 0,
    saveTime2 = 0,
    cps = 0;
function checkTime(){
    var d = new Date().getTime();
    if(d > 16.67 + saveTime){
        cps++;
        saveTime = d;
        if(d > saveTime2 + 1000){
            saveTime2 = d;
            console.log(cps);
            cps = 0;
        }
    }
}

update();
Vardan Betikyan
  • 354
  • 5
  • 20
  • `setTimeout` does not have high resolution once you get to the millisecond level. – CertainPerformance Feb 18 '21 at 02:33
  • But there's no load on the code.. also I've used `setInterval` as well, same result. I've used `setInterval` in the past with `16.67ms` with no issues before – Vardan Betikyan Feb 18 '21 at 02:36
  • If you really need this sort of precision, if I were you, I'd recursively fire a `setTimeout` with *no delay at all*. On each call, check to see if enough time has elapsed - if so, run the real function you care about. – CertainPerformance Feb 18 '21 at 02:37
  • I just put up the new method I used, tried that method as well, still getting `34`. The only thing that works properly is `requestAnimationFrame()` which holds steadly at `165fps`, but that can be used only client side – Vardan Betikyan Feb 18 '21 at 02:53
  • Could this be some sort of hardware issue? – Vardan Betikyan Feb 18 '21 at 02:59
  • 1
    Could be, 34 (30ms per timeout) sounds odd as a limit, in most situations, more should be able to fit into a second I'd have thought – CertainPerformance Feb 18 '21 at 03:01
  • Exactly! Especially that I'm using an i9 processor with a RTX2070 GPU. Should be able to handle overkill loads let alone a simple timer. Any idea what's going on? – Vardan Betikyan Feb 18 '21 at 03:07

0 Answers0