npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

test-settimeout-simple

v0.0.3

Published

Test of effective timeout value for JavaScript setTimeout() function.

Downloads

1

Readme

test-settimeout-simple

setTimeout() caveats or misunderstanding. What is real timeout value?

Usage:

To install locally go in the root directory of your project and type

npm install test-settimeout-simple --save-dev

or

npm -i test-settimeout-simple --save-dev

Test from console:

npm test <delay> <timeout>

where
delay and timeout are digital variables with meanings:
timeout - number of milliseconds similar to second argument of JavaScript setTimeout() function.
delay - presumable calculation time in milliseconds necessary to carry out codes in js-block following after the line where setTimeout() function is calling Example:

npm test 100 500

or

npm test 500 200

To get equivalent tests inside your project module:

var simp = require('test-settimeout-simple').simple;
simp.test(100,500);
simp.test(500,200);

See codes and printing output for details comprehension.

Pretext:

I observed that effective timeout (lag of launching) of argument-function in standard JavaScript setTimeout(fun, timeout) depends on the time necessary to carry out calculation of codes following the line where setTimeout() resides, i.e.

By definition

{
  // ...
  t0 = new Date();
  var cleaner = setTimeout(fun,timeout);  

means that function fun will be launched over approximately timeout milliseconds. Suppose that after this statement we have a block consuming time for it's calculation, like this

 var t = 0, tw;  
  while( t < delay ){    
    tw = new Date();
    t = tw - t0;
  }

'delay' milliseconds will be consumed by this.

The effective timeout over which fun will begin to run will be equal to timeout argument's value only if timeout >= delay

Otherwise the delay's value determines effective timeout.

Testing code used:

var cleaner,
    ob = { 
      tf: undefined  //  fun's run beginning time      
    }; 
/** 
 * setTimeout() function-argument
 */
function fun(){
  var tf = new Date();
  ob.tf = tf.getTime();
  
  console.log('in fun: tf = %s, (tf - t0) = %s',
      ob.tf,
      ob.tf - ob.t0);
 
  clearTimeout(cleaner);
}   
/** 
 * test function 
 * @param {number} delay of process's flow in [milliseconds] 
 * @param {number} timeout value of setTimout()'s second argument 
 */
function test( delay, timeout ){
  console.log('test goes ...');
  var tw,      
      t0 = new Date();
  ob.t0 = t0.getTime();
  
  cleaner = setTimeout(fun,timeout);
  
  var t = 0;  
  while( t < delay ){    // process lag
    tw = new Date();
    t = tw - t0;
  }  
  console.log('finish of test with\n' +
              'delay: %s\n' +
              'timeout: %s\n' +  
              't0 = %s\n' +
              'tw = %s\n' +                                   
              't = %s',
              delay, timeout,
              t0.getTime(),tw.getTime(), t
              );  
}

Table bellow illustrates testing results used codes from repository https://github.com/vuGitH/test-settimeout-simple :

in node.js REPL

| delay | timeout | effective timeout | |-------|-------|---------------------| | 1000 | 1000 | 1011 | | 1000 | 1000 | 1007 | | 50 | 50 | 56 | | 50 | 50 | 56 | | 100 |2000 | 2007 | | 200 | 5000 | 5001 | | 500 | 100 | 502 | | 1000 | 100 | 1007 | | 1000 | 50 | 1007 | | 1000 | 50 | 1005 | | 1000 | 50 | 1006 | | 2000 | 50 | 2006 | | 2000 | 50 | 2006 | | 2000 | 50 | 2006 | | 4000 | 50 | 4006 | | 4000 | 50 | 4006 | | 4000 | 3000 | 4007 |

in Google Chrome console

| delay | timeout | effective timeout | |-------|-------|---------------------| | 1000 | 1000 | 1003 | | 1000 | 1000 | 1001 | | 50 | 50 | 51 | | 50 | 50 | 52 | | 100 |2000 | 2000 | | 200 | 5000 | 5001 | | 500 | 100 | 502 | | 1000 | 100 | 1004 | | 1000 | 50 | 1002 | | 1000 | 50 | 1002 | | 1000 | 50 | 1002 | | 2000 | 50 | 2001 | | 2000 | 50 | 2001 | | 2000 | 50 | 2001 | | 4000 | 50 | 4003 | | 4000 | 50 | 4001 | | 4000 | 3000 | 4006 |

in MSE console

| delay | timeout | effective timeout | |-------|-------|---------------------| | 1000 | 1000 | 1003 | | 1000 | 1000 | 1004 | | 50 | 50 | 53 | | 50 | 50 | 56 | | 100 |2000 | 2004 | | 200 | 5000 | 5011 | | 500 | 100 | 505 | | 1000 | 100 | 1004 | | 1000 | 50 | 1006 | | 1000 | 50 | 1003 | | 1000 | 50 | 1003 | | 2000 | 50 | 2005 | | 2000 | 50 | 2001 | | 2000 | 50 | 2006 | | 4000 | 50 | 4003 | | 4000 | 50 | 4002 | | 4000 | 3000 | 4002 |

Comments or editions Welcome! Vladimir Uralov [email protected]