nv-facutil-simple-test

0.0.22 • Public • Published

nv-facutil-simple-test

  • nv-facutil-simple-test simple test tools

install

  • npm install nv-facutil-simple-test

usage

const {sync,async,Ahook} = require("nv-facutil-simple-test")

example

sync

function tst0(ary0,ary1) {
    ary0.concat(ary1)
}

> sync(10000000,tst0,[1,2],[3,4,5])
{ rounds: 10000000, f: [Function: tst0], costed: 2894.0468668937683 }
>

async

 function tst1(delay) {
     let p = new Promise(
         (rs,rj)=> {
             setTimeout(()=>{rs(true)},delay)
         }
     );
     return(p)
 }

 var p = async(5,tst1,3000)

 > p
 Promise { <pending> }
 > p
 Promise {
   { rounds: 5, f: [Function: tst1], costed: 15013.968754768372 }
 }
 >

compare for fastest

    class Cnode  {
        constructor(a,b) {
            this.a = a;
            this.b = b;
        }
    }

    function Fnode(a,b) {
        this.a = a;
        this.b = b;
    }

    class Anode extends Array {
        get a()  {return(this[0])}
        set a(v) {this[0]=v}
        get b()  {return(this[1])}
        set b(v) {this[1]=v}
    }

    var new_cnd = (a,b)=>new Cnode(a,b);
    var new_fnd = (a,b)=>new Fnode(a,b);
    var new_arynd = (a,b)=>new Anode(a,b);

    var obj_creat = (a,b) => {
        let nd = Object.create(Cnode.prototype)
        nd.a = a;
        nd.b = b;
        return(nd)
    }

    var nul_creat = (a,b) => {
        let nd = Object.create(null);
        nd.a = a;
        nd.b = b;
        return(nd)
    }


    var shape = { a:0,b:0}
    var plain_obj = (a,b) => ({a,b})


    var plain_ary = (a,b) => [a,b]

    var reflect = (a,b) => Reflect.construct(Fnode, [a,b]);

    class Priv {
        #a 
        #b 
        constructor(a,b) {
            this.#a = a;
            this.#b = b;
        }
    }

    var priv = (a,b) =>new Priv(a,b);


    var obj_with_gtst = (a,b) =>({
        "#a":100,
        "#b":200,
        get a() {return(this["#a"])},
        set a(v) {this["#a"] =v},
        get b() {return(this["#b"])},
        set b(v) {this["#b"] =v}
    })

    cmp(10000000,[new_cnd,new_fnd,obj_creat,nul_creat,plain_obj,new_arynd,plain_ary,reflect,priv,obj_with_gtst],111,222)

    /*
        [
          {
            rounds: 10000000,
            f: [Function: plain_obj],
            costed: 167.5416979789734
          },
          {
            rounds: 10000000,
            f: [Function: plain_ary],
            costed: 197.22762298583984
          },
          {
            rounds: 10000000,
            f: [Function: new_arynd],
            costed: 202.34725713729858
          },
          {
            rounds: 10000000,
            f: [Function: new_fnd],
            costed: 208.09321069717407
          },
          {
            rounds: 10000000,
            f: [Function: new_cnd],
            costed: 211.60849380493164
          },
          {
            rounds: 10000000,
            f: [Function: obj_creat],
            costed: 332.6821961402893
          },
          {
            rounds: 10000000,
            f: [Function: reflect],
            costed: 514.2599349021912
          },
          {
            rounds: 10000000,
            f: [Function: nul_creat],
            costed: 675.6858191490173
          },
          { rounds: 10000000, f: [Function: priv], costed: 2362.0123538970947 },
          {
            rounds: 10000000,
            f: [Function: obj_with_gtst],
            costed: 13023.659306049347
          }
        ]
    */

Ahook

  • only work in nodejs, coz it use async_hook

 var a = new Ahook();
 a.enable();

 > a.lst_
 {
   id: 222,
   type: 'TickObject',
   trigger: 5,
   resource: {
     callback: [Function: afterWriteTick],
     args: [ [Object] ],
     [Symbol(async_id_symbol)]: 222,
     [Symbol(trigger_async_id_symbol)]: 5
   }
 }
 > a.Timeout_
 [
   {
     id: 201,
     type: 'Timeout',
     trigger: 5,
     resource: Timeout {
       _idleTimeout: 15,
       _idlePrev: null,
       _idleNext: null,
       _idleStart: 75616,
       _onTimeout: [Function: flushHistory],
       _timerArgs: undefined,
       _repeat: null,
       _destroyed: true,
       [Symbol(refed)]: true,
       [Symbol(kHasPrimitive)]: false,
       [Symbol(asyncId)]: 201,
       [Symbol(triggerId)]: 5
     }
   },
   {
     id: 202,
     type: 'Timeout',
     trigger: 5,
     resource: {
       callback: [Function: maybeReadMore_],
       args: [Array],
       [Symbol(async_id_symbol)]: 202,
       [Symbol(trigger_async_id_symbol)]: 5
     }
   },
   ......
 ]

 a.disable();

METHODS

Ahook.prototype.enable
Ahook.prototype.disable
Ahook.prototype.clear

getter

Ahook.prototype.FSEVENTWRAP_          Ahook.prototype.FSREQCALLBACK_
Ahook.prototype.GETADDRINFOREQWRAP_   Ahook.prototype.GETNAMEINFOREQWRAP_
Ahook.prototype.HTTPCLIENTREQUEST_    Ahook.prototype.HTTPINCOMINGMESSAGE_
Ahook.prototype.Immediate_            Ahook.prototype.JSSTREAM_
Ahook.prototype.Microtask_            Ahook.prototype.PBKDF2REQUEST_
Ahook.prototype.PIPECONNECTWRAP_      Ahook.prototype.PIPEWRAP_
Ahook.prototype.PROCESSWRAP_          Ahook.prototype.PROMISE_
Ahook.prototype.QUERYWRAP_            Ahook.prototype.RANDOMBYTESREQUEST_
Ahook.prototype.SHUTDOWNWRAP_         Ahook.prototype.SIGNALWRAP_
Ahook.prototype.SSLCONNECTION_        Ahook.prototype.STATWATCHER_
Ahook.prototype.TCPCONNECTWRAP_       Ahook.prototype.TCPSERVERWRAP_
Ahook.prototype.TCPWRAP_              Ahook.prototype.TLSWRAP_
Ahook.prototype.TTYWRAP_              Ahook.prototype.TickObject_
Ahook.prototype.Timeout_              Ahook.prototype.UDPSENDWRAP_
Ahook.prototype.UDPWRAP_              Ahook.prototype.WRITEWRAP_
Ahook.prototype.ZLIB_                 

Ahook.prototype.all_
Ahook.prototype.length_               
Ahook.prototype.lst_

API

  • sync(rounds,f,...args)
  • async(rounds,f,...args)
  • cmp(rounds,func_ary,...args)
  • acmp(rounds,func_ary,...args)
  • fastest(rounds,func_ary,...args)
  • afastest(rounds,func_ary,...args)

TESTS

++ VS i =i+1

    > var i =0
    > var f0 = ()=>i++                 //a little little slightly faster,can be ignored
    > var f1 = ()=>i=i+1

    > const {sync} = require("nv-facutil-simple-test");
    > sync(10000000,f0)
    { rounds: 10000000, f: [Function: f0], costed: 127.74292135238647 }
    > sync(10000000,f1)
    { rounds: 10000000, f: [Function: f1], costed: 130.06517267227173 }
    > sync(10000000,f0)
    { rounds: 10000000, f: [Function: f0], costed: 139.97180604934692 }
    > sync(10000000,f1)
    { rounds: 10000000, f: [Function: f1], costed: 130.97596168518066 }
    > sync(10000000,f0)
    { rounds: 10000000, f: [Function: f0], costed: 132.0758399963379 }
    > sync(10000000,f1)
    { rounds: 10000000, f: [Function: f1], costed: 144.76730298995972 }
    >
    > sync(100000000,f0)
    { rounds: 100000000, f: [Function: f0], costed: 1218.9988231658936 }
    > sync(100000000,f1)
    { rounds: 100000000, f: [Function: f1], costed: 1279.4322390556335 }
    > sync(100000000,f0)
    { rounds: 100000000, f: [Function: f0], costed: 1324.279109954834 }
    > sync(100000000,f1)
    { rounds: 100000000, f: [Function: f1], costed: 1343.4789361953735 }
    >
    > sync(100000000,f0)
    { rounds: 100000000, f: [Function: f0], costed: 1257.3673729896545 }
    > sync(100000000,f1)
    { rounds: 100000000, f: [Function: f1], costed: 1372.1064085960388 }
    >

gt

    > var f0 = (a,b)=>a>b;
    > var f1 = (i)=>i++;


    > sync(10000000,f0,100,200)
    { rounds: 10000000, f: [Function: f0], costed: 215.06074380874634 }
    > sync(10000000,f1,3)
    { rounds: 10000000, f: [Function: f1], costed: 236.30819702148438 }
    
    
    > i =0
    0
    > var f2 = ()=>i++;
    > sync(10000000,f2)
    > sync(10000000,f0,100,200)
    { rounds: 10000000, f: [Function: f0], costed: 209.85319900512695 }
    >

    { rounds: 10000000, f: [Function: f2], costed: 235.23615026474 }

array

    const x = require("nv-facutil-simple-test")  

push AND shift

    function push_and_then_shift(arr,v) {
        arr.push(v)
        arr.shift()
    }

    function just_move_ele(arr,v) {
        let lngth = arr.length;
        for(let i=0;i<lngth-1;i++) {
            arr[i]= arr[i+1]
        }
        arr[lngth-1] = v
    }


    > push_and_then_shift(arr0,8888)
    > arr0
    [ 20, 30, 40, 8888 ]
    >
    > just_move_ele(arr0,7777)
    > arr0
    [ 30, 40, 8888, 7777 ]
    >
mini size 0~6 pppnext pprev prev current next nnext nnnext
    //seemed  just_move_ele MAYBE  faster

    var arr = [10,20,30,40]

    > x.fastest(10000000,[push_and_then_shift,just_move_ele],arr0,8888)
    {
      rounds: 10000000,
      f: [Function: just_move_ele],
      costed: 161.05021619796753
    }
    >


    > x.cmp(10000000,[push_and_then_shift,just_move_ele],arr0,8888)
    [
      {
        rounds: 10000000,
        f: [Function: just_move_ele],
        costed: 162.34449529647827
      },
      {
        rounds: 10000000,
        f: [Function: push_and_then_shift],
        costed: 385.70706367492676
      }
    ]
    > x.sync(10000000,just_move_ele,arr0,888)
    {
      rounds: 10000000,
      f: [Function: just_move_ele],
      costed: 202.35962390899658
    }
    > x.sync(10000000,push_and_then_shift,arr0,888)
    {
      rounds: 10000000,
      f: [Function: push_and_then_shift],
      costed: 382.8467917442322
    }
    >

small size 128

    //seemed push_and_then_shift MAYBE faster

    var arr0 = Array.from({length:128}).map((r,i)=>i)
   

    > x.fastest(10000000,[push_and_then_shift,just_move_ele],arr0,8888)
    {
      rounds: 10000000,
      f: [Function: push_and_then_shift],
      costed: 1024.305088043213
    }
    > x.cmp(10000000,[push_and_then_shift,just_move_ele],arr0,8888)
    [
      {
        rounds: 10000000,
        f: [Function: push_and_then_shift],
        costed: 947.6514468193054
      },
      {
        rounds: 10000000,
        f: [Function: just_move_ele],
        costed: 1104.9360208511353
      }
    ]
    >
    > x.sync(10000000,just_move_ele,arr0,888)
    {
      rounds: 10000000,
      f: [Function: just_move_ele],
      costed: 1109.8695631027222
    }
    > x.sync(10000000,push_and_then_shift,arr0,888)
    {
      rounds: 10000000,
      f: [Function: push_and_then_shift],
      costed: 999.6719512939453
    }
    >

other size

    var arr0 = Array.from({length:2**26}).map((r,i)=>i)

    x.fastest(100,[push_and_then_shift,just_move_ele],arr0,8888)
    > x.fastest(100,[push_and_then_shift,just_move_ele],arr0,8888)
    {
      rounds: 100,
      f: [Function: push_and_then_shift],
      costed: 6895.157344341278
    }

    var arr0 = Array.from({length:2**16}).map((r,i)=>i)

    > x.fastest(100000,[push_and_then_shift,just_move_ele],arr0,8888)
    {
      rounds: 100000,
      f: [Function: push_and_then_shift],
      costed: 2241.0950689315796
    }
    >

    var arr0 = Array.from({length:2**15}).map((r,i)=>i)

    x.fastest(1000000,[push_and_then_shift,just_move_ele],arr0,8888)

    > x.fastest(1000000,[push_and_then_shift,just_move_ele],arr0,8888)
    {
      rounds: 1000000,
      f: [Function: push_and_then_shift],
      costed: 9688.172526836395
    }
    >


    > var arr0 = Array.from({length:2**14}).map((r,i)=>i)

    >
    > x.fastest(1000000,[push_and_then_shift,just_move_ele],arr0,8888)
    {
      rounds: 1000000,
      f: [Function: push_and_then_shift],
      costed: 4321.872590065002
    }
    >

    var arr0 = Array.from({length:2**13}).map((r,i)=>i)

    x.fastest(1000000,[push_and_then_shift,just_move_ele],arr0,8888)

    > var arr0 = Array.from({length:2**13}).map((r,i)=>i)
    >
    > x.fastest(1000000,[push_and_then_shift,just_move_ele],arr0,8888)
    {
      rounds: 1000000,
      f: [Function: push_and_then_shift],
      costed: 113.6801929473877
    }
    >


    var arr0 = Array.from({length:2**12}).map((r,i)=>i)

    x.fastest(1000000,[push_and_then_shift,just_move_ele],arr0,8888)

    > x.fastest(1000000,[push_and_then_shift,just_move_ele],arr0,8888)
    {
      rounds: 1000000,
      f: [Function: push_and_then_shift],
      costed: 139.2634081840515
    }


    var arr0 = Array.from({length:2**11}).map((r,i)=>i)

    x.fastest(1000000,[push_and_then_shift,just_move_ele],arr0,8888)
    > x.fastest(1000000,[push_and_then_shift,just_move_ele],arr0,8888)
    {
      rounds: 1000000,
      f: [Function: push_and_then_shift],
      costed: 112.35720729827881
    }


    var arr0 = Array.from({length:2**10}).map((r,i)=>i)

    x.fastest(1000000,[push_and_then_shift,just_move_ele],arr0,8888)

    > x.fastest(1000000,[push_and_then_shift,just_move_ele],arr0,8888)
    {
      rounds: 1000000,
      f: [Function: push_and_then_shift],
      costed: 109.84335327148438
    }
    >

    var arr0 = Array.from({length:2**9}).map((r,i)=>i)
    x.fastest(1000000,[push_and_then_shift,just_move_ele],arr0,8888)

    /*
    > x.fastest(1000000,[push_and_then_shift,just_move_ele],arr0,8888)
    {
      rounds: 1000000,
      f: [Function: push_and_then_shift],
      costed: 104.96030282974243
    }
    > x.cmp(1000000,[push_and_then_shift,just_move_ele],arr0,8888)
    [
      {
        rounds: 1000000,
        f: [Function: push_and_then_shift],
        costed: 109.10006427764893
      },
      {
        rounds: 1000000,
        f: [Function: just_move_ele],
        costed: 354.9349093437195
      }
    ]
    >

    */

    var arr0 = Array.from({length:2**8}).map((r,i)=>i)
    x.fastest(10000000,[push_and_then_shift,just_move_ele],arr0,8888)
    x.cmp(10000000,[push_and_then_shift,just_move_ele],arr0,8888)

    /*
    > x.fastest(10000000,[push_and_then_shift,just_move_ele],arr0,8888)
    {
      rounds: 10000000,
      f: [Function: push_and_then_shift],
      costed: 965.2179284095764
    }
    > x.cmp(10000000,[push_and_then_shift,just_move_ele],arr0,8888)
    [
      {
        rounds: 10000000,
        f: [Function: push_and_then_shift],
        costed: 1006.8404927253723
      },
      {
        rounds: 10000000,
        f: [Function: just_move_ele],
        costed: 1912.6992869377136
      }
    ]
    >

    */


    var arr0 = Array.from({length:2**7}).map((r,i)=>i)
    x.fastest(10000000,[push_and_then_shift,just_move_ele],arr0,8888)
    x.cmp(10000000,[push_and_then_shift,just_move_ele],arr0,8888)

    /*
    > var arr0 = Array.from({length:2**7}).map((r,i)=>i)
    > x.fastest(10000000,[push_and_then_shift,just_move_ele],arr0,8888)
    {
      rounds: 10000000,
      f: [Function: push_and_then_shift],
      costed: 975.0886740684509
    }
    > x.cmp(10000000,[push_and_then_shift,just_move_ele],arr0,8888)
    [
      {
        rounds: 10000000,
        f: [Function: push_and_then_shift],
        costed: 1021.1277914047241
      },
      {
        rounds: 10000000,
        f: [Function: just_move_ele],
        costed: 1123.0839891433716
      }
    ]
    >
    */


    var arr0 = Array.from({length:2**6}).map((r,i)=>i)
    x.fastest(10000000,[push_and_then_shift,just_move_ele],arr0,8888)
    x.cmp(10000000,[push_and_then_shift,just_move_ele],arr0,8888)


    /*
    > x.fastest(10000000,[push_and_then_shift,just_move_ele],arr0,8888)
    {
      rounds: 10000000,
      f: [Function: push_and_then_shift],
      costed: 506.3333601951599
    }
    > x.cmp(10000000,[push_and_then_shift,just_move_ele],arr0,8888)
    [
      {
        rounds: 10000000,
        f: [Function: push_and_then_shift],
        costed: 634.4168033599854
      },
      {
        rounds: 10000000,
        f: [Function: just_move_ele],
        costed: 697.2383561134338
      }
    ]
    >

    */


    var arr0 = Array.from({length:2**5}).map((r,i)=>i)
    x.fastest(10000000,[push_and_then_shift,just_move_ele],arr0,8888)
    x.cmp(10000000,[push_and_then_shift,just_move_ele],arr0,8888)

    /*
    > var arr0 = Array.from({length:2**5}).map((r,i)=>i)
    undefined
    > x.fastest(10000000,[push_and_then_shift,just_move_ele],arr0,8888)
    {
      rounds: 10000000,
      f: [Function: push_and_then_shift],
      costed: 270.8724579811096
    }
    > x.cmp(10000000,[push_and_then_shift,just_move_ele],arr0,8888)
    [
      {
        rounds: 10000000,
        f: [Function: push_and_then_shift],
        costed: 266.78145694732666
      },
      {
        rounds: 10000000,
        f: [Function: just_move_ele],
        costed: 378.65622091293335
      }
    ]
    >

    */


    var arr0 = Array.from({length:2**4}).map((r,i)=>i)
    x.fastest(10000000,[push_and_then_shift,just_move_ele],arr0,8888)
    x.cmp(10000000,[push_and_then_shift,just_move_ele],arr0,8888)

    /*
    > x.fastest(10000000,[push_and_then_shift,just_move_ele],arr0,8888)
    {
      rounds: 10000000,
      f: [Function: push_and_then_shift],
      costed: 228.02108192443848
    }
    > x.cmp(10000000,[push_and_then_shift,just_move_ele],arr0,8888)
    [
      {
        rounds: 10000000,
        f: [Function: push_and_then_shift],
        costed: 259.8764510154724
      },
      {
        rounds: 10000000,
        f: [Function: just_move_ele],
        costed: 264.2375192642212
      }
    ]
    >
    */

    var arr0 = Array.from({length:2**3}).map((r,i)=>i)
    x.fastest(100000000,[push_and_then_shift,just_move_ele],arr0,8888)
    x.cmp(100000000,[push_and_then_shift,just_move_ele],arr0,8888)

    /*
    > x.fastest(100000000,[push_and_then_shift,just_move_ele],arr0,8888)
    {
      rounds: 100000000,
      f: [Function: just_move_ele],
      costed: 1979.1184711456299
    }
    > x.cmp(100000000,[push_and_then_shift,just_move_ele],arr0,8888)
    [
      {
        rounds: 100000000,
        f: [Function: just_move_ele],
        costed: 2022.5493369102478
      },
      {
        rounds: 100000000,
        f: [Function: push_and_then_shift],
        costed: 2156.524836063385
      }
    ]
    >

    */

prop OR key size

    const {sync,mem_change} = require("nv-facutil-simple-test")

    class GPS0 {
        constructor(lat,lon,alt) {
            this.latitude   = lat;
            this.longitude  = lon;
            this.altitude   = alt;
        }
    }


    class GPS1 {
        constructor(lat,lon,alt) {
            this.x  = lat;
            this.y  = lon;
            this.z  = alt;
        }
        get latitude_()  {return(this.x)}
        get longitude_() {return(this.y)}
        get altitude_()  {return(this.z)}
    }



    function f0() {return(new GPS0(100,100,100))}
    function f1() {return(new GPS1(100,100,100))}

    sync(5000000,f0)
    sync(5000000,f1)

    /* creat speed nearly same
    > sync(5000000,f0)
    { rounds: 5000000, f: [Function: f0], costed: 88.10719192028046 }
    > sync(5000000,f1)
    { rounds: 5000000, f: [Function: f1], costed: 86.56737101078033 }
    >
    > sync(5000000,f0)
    { rounds: 5000000, f: [Function: f0], costed: 87.2494729757309 }
    > sync(5000000,f1)
    { rounds: 5000000, f: [Function: f1], costed: 89.2833321094513 }
    >

    */


    > process.memoryUsage()
    {
      rss: 46972928,
      heapTotal: 8511488,
      heapUsed: 6355072,
      external: 1058023,
      arrayBuffers: 17147
    }
    >
    > mem_change()
    {
      rss: -2600960,
      heapTotal: -3145728,
      heapUsed: -1426304,
      external: 4,
      arrayBuffers: -15
    }

    > mem_change()
    { rss: 0, heapTotal: 0, heapUsed: 83224, external: 4, arrayBuffers: 4 }
    >


    var arr0 = Array.from({length:1000000})
    var arr1 = Array.from({length:1000000})



    > mem_change()
    {
      rss: 14991360,
      heapTotal: 16277504,
      heapUsed: 15118816,
      external: -1823,
      arrayBuffers: -1056
    }
    >




    arr0.forEach((r,i)=>{arr0[i]=f0()})



    >  mem_change()
    {
      rss: 72437760,
      heapTotal: 63700992,
      heapUsed: 47852128,
      external: -1138,
      arrayBuffers: 1
    }
    >


    arr1.forEach((r,i)=>{arr0[i]=f1()})

    >  mem_change()
    {
      rss: 35741696,
      heapTotal: 31457280,
      heapUsed: 33430368,           //
      external: 35,
      arrayBuffers: 36
    }
    >

instanceof VS constructor.name

    var u8a = new Uint8Array(4);
    var f0 = (o)=>(o instanceof Uint8Array);
    var f1 = (o)=>o.constructor.name === 'Uint8Array';

    > f0(u8a)
    true
    > f1(u8a)
    true
    >
    > x.sync(10000000,f0,u8a)
    { rounds: 10000000, f: [Function: f0], costed: 148.99506187438965 }
    >
    > x.sync(10000000,f1,u8a)
    { rounds: 10000000, f: [Function: f1], costed: 385.277530670166 }
    > x.sync(20000000,f0,u8a)
    { rounds: 20000000, f: [Function: f0], costed: 294.7771701812744 }
    >
    > x.sync(20000000,f1,u8a)
    { rounds: 20000000, f: [Function: f1], costed: 673.4802875518799 }
    >

getter speed test

    const {sync} = require("../index");

    var ra = ["v0","v1"]

    const get_from_rawa_with_index  = ()=>[ra[0],ra[1]]

    sync(10000000,get_from_rawa_with_index)


    /*
    > sync(10000000,get_from_rawa_with_index)
    {
      rounds: 10000000,
      f: [Function: get_from_rawa_with_index],
      costed: 210.45516967773438
    }
    > sync(10000000,get_from_rawa_with_index)
    {
      rounds: 10000000,
      f: [Function: get_from_rawa_with_index],
      costed: 211.53196907043457
    }
    > sync(10000000,get_from_rawa_with_index)
    {
      rounds: 10000000,
      f: [Function: get_from_rawa_with_index],
      costed: 208.35104370117188
    }
    > sync(10000000,get_from_rawa_with_index)
    {
      rounds: 10000000,
      f: [Function: get_from_rawa_with_index],
      costed: 216.23346519470215
    }
    >
    */


    class EA extends Array {
        constructor(v0,v1) {
            super();
                this.push(v0,v1)
            }
            get v0() {return(this[0])}
            get v1() {return(this[1])}
    }

    var ea   = new EA("v0","v1")

    const get_from_ea_with_index  = ()=>[ea[0],ea[1]]

    sync(10000000,get_from_ea_with_index)

    /*
    > sync(10000000,get_from_ea_with_index)
    {
      rounds: 10000000,
      f: [Function: get_from_ea_with_index],
      costed: 202.84191703796387
    }
    > sync(10000000,get_from_ea_with_index)
    {
      rounds: 10000000,
      f: [Function: get_from_ea_with_index],
      costed: 215.38772010803223
    }
    > sync(10000000,get_from_ea_with_index)
    {
      rounds: 10000000,
      f: [Function: get_from_ea_with_index],
      costed: 214.39294815063477
    }
    > sync(10000000,get_from_ea_with_index)
    {
      rounds: 10000000,
      f: [Function: get_from_ea_with_index],
      costed: 210.52841758728027
    }
    >
    */

    const get_from_ea_with_getter = ()=>[ea.v0,ea.v1]
    sync(10000000,get_from_ea_with_getter)
    /*
    > sync(10000000,get_from_ea_with_getter)
    {
      rounds: 10000000,
      f: [Function: get_from_ea_with_getter],
      costed: 208.8599624633789
    }
    > sync(10000000,get_from_ea_with_getter)
    {
      rounds: 10000000,
      f: [Function: get_from_ea_with_getter],
      costed: 199.53731155395508
    }
    > sync(10000000,get_from_ea_with_getter)
    {
      rounds: 10000000,
      f: [Function: get_from_ea_with_getter],
      costed: 205.06519317626953
    }
    > sync(10000000,get_from_ea_with_getter)
    {
      rounds: 10000000,
      f: [Function: get_from_ea_with_getter],
      costed: 209.24454879760742
    }
    >
    */



    var rd   = {v0:"v0",v1:"v1"}

    const get_from_rd = ()=> [rd.v0,rd.v1]

    sync(10000000,get_from_rd)

    /*
    > sync(10000000,get_from_rd)
    {
      rounds: 10000000,
      f: [Function: get_from_rd],
      costed: 201.54963493347168
    }
    > sync(10000000,get_from_rd)
    {
      rounds: 10000000,
      f: [Function: get_from_rd],
      costed: 203.9611873626709
    }
    > sync(10000000,get_from_rd)
    {
      rounds: 10000000,
      f: [Function: get_from_rd],
      costed: 211.65743827819824
    }
    >
    */


    var mp  = new Map(Object.entries(rd));

    const get_from_mp = () =>[mp.get("v0"),mp.get("v1")]


    sync(10000000,get_from_mp)

    /*
    > sync(10000000,get_from_mp)
    {
      rounds: 10000000,
      f: [Function: get_from_mp],
      costed: 431.3168029785156
    }
    > sync(10000000,get_from_mp)
    {
      rounds: 10000000,
      f: [Function: get_from_mp],
      costed: 436.4684009552002
    }
    > sync(10000000,get_from_mp)
    {
      rounds: 10000000,
      f: [Function: get_from_mp],
      costed: 453.76171684265137
    }
    > sync(10000000,get_from_mp)
    {
      rounds: 10000000,
      f: [Function: get_from_mp],
      costed: 470.9776554107666
    }
    >
    */


    class D {
        v0="v0"
        v1="v1"
    }

    var d = new D()

    const get_from_d = ()=> [d.v0,d.v1]
    /*
    > sync(10000000,get_from_d)
    {
      rounds: 10000000,
      f: [Function: get_from_d],
      costed: 201.63967323303223
    }
    > sync(10000000,get_from_d)
    {
      rounds: 10000000,
      f: [Function: get_from_d],
      costed: 200.51382064819336
    }
    > sync(10000000,get_from_d)
    {
      rounds: 10000000,
      f: [Function: get_from_d],
      costed: 201.8022174835205
    }
    > sync(10000000,get_from_d)
    {
      rounds: 10000000,
      f: [Function: get_from_d],
      costed: 228.96243858337402
    }
    >
    */



    class PA extends Array {
        constructor(v0,v1) {
            super();
            this.push(v0,v1)
        }
    }

    Object.defineProperty(PA.prototype,"v0",{get:function(){return(this[0])}})
    Object.defineProperty(PA.prototype,"v1",{get:function(){return(this[1])}})


    var pa = new PA("v0","v1")
    /*
    > pa
    PA(2) [ 'v0', 'v1' ]
    >
    */

    const get_from_pa_with_index   = ()=>[pa[0],pa[1]]
    /*
    > sync(10000000,get_from_pa_with_index)
    {
      rounds: 10000000,
      f: [Function: get_from_pa_with_index],
      costed: 207.52319526672363
    }
    > sync(10000000,get_from_pa_with_index)
    {
      rounds: 10000000,
      f: [Function: get_from_pa_with_index],
      costed: 212.59967613220215
    }
    > sync(10000000,get_from_pa_with_index)
    {
      rounds: 10000000,
      f: [Function: get_from_pa_with_index],
      costed: 211.38087272644043
    }
    > sync(10000000,get_from_pa_with_index)
    {
      rounds: 10000000,
      f: [Function: get_from_pa_with_index],
      costed: 222.359037399292
    }
    >
    */
    const get_from_pa_with_getter  = ()=>[pa.v0,pa.v1]

    /*
    > sync(10000000,get_from_pa_with_getter)
    {
      rounds: 10000000,
      f: [Function: get_from_pa_with_getter],
      costed: 207.68007278442383
    }
    > sync(10000000,get_from_pa_with_getter)
    {
      rounds: 10000000,
      f: [Function: get_from_pa_with_getter],
      costed: 208.297456741333
    }
    > sync(10000000,get_from_pa_with_getter)
    {
      rounds: 10000000,
      f: [Function: get_from_pa_with_getter],
      costed: 223.48654747009277
    }
    > sync(10000000,get_from_pa_with_getter)
    {
      rounds: 10000000,
      f: [Function: get_from_pa_with_getter],
      costed: 226.50678253173828
    }
    > sync(10000000,get_from_pa_with_getter)
    {
      rounds: 10000000,
      f: [Function: get_from_pa_with_getter],
      costed: 211.12835502624512
    }
    >
    */

    var a = ["v0","v1"]

    var px = new Proxy(
        a,
        {
            get: function(target, property, receiver) {
                 if(property === "v0") {
                      return(Reflect.get(target,0))
                 } else if(property === "v1") {
                      return(Reflect.get(target,1))
                 } else {
                      return(Reflect.get(target,property))
                 }
            }
        }
    )

    /*
    > px.v0
    'v0'
    > px.v1
    'v1'
    > px[0]
    'v0'
    > px[1]
    'v1'
    >
    */

    const get_from_px_with_index  = ()=>[px[0],px[1]]
    /*
    > sync(10000000,get_from_px_with_index)
    {
      rounds: 10000000,
      f: [Function: get_from_px_with_index],
      costed: 6042.160470962524
    }
    > sync(10000000,get_from_px_with_index)
    {
      rounds: 10000000,
      f: [Function: get_from_px_with_index],
      costed: 6012.279012680054
    }
    >

    */

    const get_from_px_with_prop  = ()=>[px.v0,px.v1]
    /*
    > sync(10000000,get_from_px_with_prop)
    {
      rounds: 10000000,
      f: [Function: get_from_px_with_prop],
      costed: 1954.2461643218994
    }
    > sync(10000000,get_from_px_with_prop)
    {
      rounds: 10000000,
      f: [Function: get_from_px_with_prop],
      costed: 1921.415605545044
    }
    > sync(10000000,get_from_px_with_prop)
    {
      rounds: 10000000,
      f: [Function: get_from_px_with_prop],
      costed: 1951.849027633667
    }
    >
    */

big fixed data length speed test

        const rnd_sen = require("nv-random-sentence");


        var D = {}
        const creat_frag = (D,idx) =>{
            let d = {}
            for(let i=0;i<500000;i++) {
               let k = rnd_sen.NVN_
               d[k]  = rnd_sen.NVN_
                   D[k]  = d[k]
            }   
                fs.writeFileSync(`frag${idx}.json`,JSON.stringify(d))
        }


        for(let idx=0;idx<10;idx++) {
            creat_frag(D,idx)
        }

        TEST# ls -l
        total 275796
        -rw-r--r-- 1 root root 28234445 Oct 19 15:33 frag0.json
        -rw-r--r-- 1 root root 28243940 Oct 19 15:33 frag1.json
        -rw-r--r-- 1 root root 28241764 Oct 19 15:33 frag2.json
        -rw-r--r-- 1 root root 28240741 Oct 19 15:33 frag3.json
        -rw-r--r-- 1 root root 28245468 Oct 19 15:33 frag4.json
        -rw-r--r-- 1 root root 28239630 Oct 19 15:33 frag5.json
        -rw-r--r-- 1 root root 28240529 Oct 19 15:33 frag6.json
        -rw-r--r-- 1 root root 28239675 Oct 19 15:34 frag7.json
        -rw-r--r-- 1 root root 28232199 Oct 19 15:34 frag8.json
        -rw-r--r-- 1 root root 28237992 Oct 19 15:34 frag9.json
        TEST# 


        > Object.keys(D).length
        5000000
        > 

        const len = (D)=>Object.keys(D).length;

        /*
        > sync(10,len,D)
        { rounds: 10, f: [Function: len], costed: 41091.69201660156 }       //計算一個length就需要4秒
        > 
        */

        const get = (k)=>D[k];

        > get('dialyzer_chouse_brill')
        'callboard_imitate_estaminet'
        > 

        > sync(10000000,get,'dialyzer_chouse_brill')
        { rounds: 10000000, f: [Function: get], costed: 253.25781440734863 }
        > 

        .exit



        var entries = Array(5000000);
        for(let i=0;i<5000000;i++) {
            entries[i] = ['','']
        }

        var c = 0
        for(let idx=0;idx<10;idx++) {
            let frag = JSON.parse(fs.readFileSync(`frag${idx}.json`).toString());
                for(let k in frag) {
                     entries[c] = [k,frag[k]]
                         c=c+1;
                }
        }

        > entries.length                                                    
        5000000



        const len = (es)=>es.length;
        > sync(10,len,entries)
        { rounds: 10, f: [Function: len], costed: 0.028247833251953125 }    //計算length基本不需要時間
        > 





        > entries[2500000]
        [ 'dialyzer_chouse_brill', 'callboard_imitate_estaminet' ]
        > 

        const get = (k)=> {
            for(let i=0;i<5000000;i++) {
                    if(entries[i][0] === k) {
                            return(entries[i][1])
                        }
                }
        }

        > get('dialyzer_chouse_brill')
        'callboard_imitate_estaminet'
        > 

        > sync(10,get,'dialyzer_chouse_brill')
        { rounds: 10, f: [Function: get], costed: 263.88294410705566 }             //耗時不可接受 單次需要25毫秒
        > 

        .exit 




        var mp = new Map();
        for(let idx=0;idx<10;idx++) {
            let frag = JSON.parse(fs.readFileSync(`frag${idx}.json`).toString());
                for(let k in frag) {
                     mp.set(k,frag[k])
                }
        }

        const len = (mp)=>mp.size;
        > sync(10,len,mp)
        { rounds: 10, f: [Function: len], costed: 0.009061813354492188 }       //計算length基本不需要時間
        > 

        const get = (k)=> {
             return(mp.get(k))
        }
        > get('dialyzer_chouse_brill')
        'callboard_imitate_estaminet'
        > 

        > sync(10000000,get,'dialyzer_chouse_brill')
        { rounds: 10000000, f: [Function: get], costed: 390.18969535827637 }          //1千萬次調用get 400ms,單次耗時忽略不記
        > 


        .exit


        var entries = Array(5000000);
        for(let i=0;i<5000000;i++) {
            entries[i] = ['','']
        }

        var c = 0
        for(let idx=0;idx<10;idx++) {
            let frag = JSON.parse(fs.readFileSync(`frag${idx}.json`).toString());
                for(let k in frag) {
                     entries[c] = [k,frag[k]]
                         entries[k] = frag[k]
                         c=c+1;
                }
        }

ary_destructor IF you sure the right-hand IS a array with the default iterator-implement

	var PAIR_WITH_ARY_TYPE_MARK = [100,200];

	var FST;
	var SND;


	var f0 = () => {
	    [FST,SND] = PAIR_WITH_ARY_TYPE_MARK;
	}

	var f1 = () => {
	    FST = PAIR_WITH_ARY_TYPE_MARK[0];
	    SND = PAIR_WITH_ARY_TYPE_MARK[1];
	}


	console.log(sync(100000000,f0))
	console.log(sync(100000000,f1))

	/*
	> console.log(sync(100000000,f0))
	{ rounds: 100000000, f: [Function: f0], costed: 1398.456472992897 }

	> console.log(sync(100000000,f1))
	{ rounds: 100000000, f: [Function: f1], costed: 1185.4215869903564 }
	   //---  this is because , the destrotor need to call iterator
	*/


	var ary = [1,2,3,4,5,6];
	var FST;
	var SND;
	var TRD;
	var QUA;
	var PNT;
	var HEX;

	var f0 = () => {[FST,SND,TRD,QUA,PNT,HEX] = ary;}
	var f1 = () => {
	    FST = ary[0];
	    SND = ary[1];
	    TRD = ary[2];
	    QUA = ary[3];
	    PNT = ary[4];
	    HEX = ary[5];
	}


	console.log(sync(100000000,f0))
	console.log(sync(100000000,f1))

	/*
	{ rounds: 100000000, f: [Function: f0], costed: 1607.1520830392838 }
	{ rounds: 100000000, f: [Function: f1], costed: 897.0708520412445 }  
	   // 被解構的結構越複雜 這個差異越明顯

	*/

for-loop-on-64k-string

	10000
	{
	  rounds: 65536,
	  f: [Function: using_char_code_at],
	  costed: 5297.109977960587
	}
	{
	  rounds: 65536,
	  f: [Function: using_many_if],
	  costed: 6676.725782871246
	}
	{
	  rounds: 65536,
	  f: [Function: using_many_case],
	  costed: 6721.5950429439545
	}
	{ rounds: 65536, f: [Function: using_tbl], costed: 18946.27202105522 }

BigUInt64Array

	const make_using_u16a = (dt=new Date) => {
	    let mts = dt.getTime();
	    let r0  = mts %   4294967296;
	    let q0  = (mts - r0)/4294967296;
	    let r1  = r0 % (65536);
	    let  q1 = (r0-r1) /65536;
		let u16a = new Uint16Array(4);
		u16a[3] = 0x7ffa;
	    u16a[2]=q0;
	    u16a[1]=q1;
	    u16a[0]=r1;
	    return(new Uint8Array(u16a.buffer))
	}
	const make_using_u32a = (dt=new Date) => {
	    let mts = dt.getTime();
	    let r0  = mts %   4294967296;
	    let q0  = (mts - r0)/4294967296;
		let u32a = new Uint32Array(2);
	    u32a[1] = 0x7ffa0000 | q0;
		u32a[0] = r0 ;
	    return(new Uint8Array(u32a.buffer))
	}
	const make_using_u64a = (dt=new Date) => {
	    let mts = dt.getTime();
	    let u64a = new BigUint64Array(1);
	    u64a[0]        = BigInt(mts);
	    let u16a = new Uint16Array(u64a.buffer);
	    u16a[3]  = 0x7ffa;
	    return(new Uint8Array(u64a.buffer))
	}


	const {sync} = require("nv-facutil-simple-test");


	console.log(sync(10000000,make_using_u16a))
	console.log(sync(10000000,make_using_u32a))
	console.log(sync(10000000,make_using_u64a))


	/*
	{
	  rounds: 10000000,
	  f: [Function: make_using_u16a],
	  costed: 6253.4816637039185
	}
	{
	  rounds: 10000000,
	  f: [Function: make_using_u32a],
	  costed: 6245.351312637329
	}
	{
	  rounds: 10000000,
	  f: [Function: make_using_u64a],
	  costed: 7142.871691703796
	}
	*/

updt

	class V {
	    _v =888
		set v(val) { console.log("setter enter");this._v=val;    console.log("setter exit");} 
		get v()    { console.log("getter enter");return(this._v);}
	}

	/*


	> ++v.v
	getter enter
	setter enter
	setter exit
	889
	> v
	V { _v: 889 }
	> 
	> v.v++
	getter enter
	setter enter
	setter exit
	889
	> v
	V { _v: 890 }

	> v.v = 0
	setter enter
	setter exit
	0

	*/

	/*
	> (1+v.v++)
	getter enter
	setter enter
	setter exit
	1
	> v.v = 0
	setter enter
	setter exit
	0
	> 
	*/

	/*
	> (1+ ++v.v)
	getter enter
	setter enter
	setter exit
	2
	> (((v.v)))++
	getter enter
	setter enter
	setter exit
	1
	> 
	*/


			class V1 {
                _v =888
                    set v(val) { this._v=val;}
                    get v()    { return(this._v);}
            }

            var v  = 888;
            var v1 = new V1();
			var v2 = {v:888}

            var f0 = ()=>++v;
            var f1 = ()=>++v1.v;
			var f2 = ()=>++v2.v;

            console.log(sync(1000000000,f0))
			//{ rounds: 1000000000, f: [Function: f0], costed: 12089.421229362488 }
            console.log(sync(1000000000,f1))
			//{ rounds: 1000000000, f: [Function: f1], costed: 12359.855974197388 }
			console.log(sync(1000000000,f2))
			//{ rounds: 1000000000, f: [Function: f2], costed: 7612.168771743774 }
            /*
               > v
               1000000888
               > v1
               V1 { _v: 1000000888 }
               > v2
               { v: 1000000888 }
               > 
			*/
			
			console.log(sync(1000000000,f0))
			//{ rounds: 1000000000, f: [Function: f0], costed: 7590.657766342163 }
			console.log(sync(1000000000,f1))
			//{ rounds: 1000000000, f: [Function: f1], costed: 7561.783745765686 }
			console.log(sync(1000000000,f2))
			//{ rounds: 1000000000, f: [Function: f2], costed: 11850.260585784912 }
			
			console.log(sync(1000000000,f0))
			//{ rounds: 1000000000, f: [Function: f0], costed: 19522.066294670105 }
			console.log(sync(1000000000,f1))
			//{ rounds: 1000000000, f: [Function: f1], costed: 9180.399136543274 }
			console.log(sync(1000000000,f2))
			//{ rounds: 1000000000, f: [Function: f2], costed: 13682.482397079468 }
			
			console.log(sync(1000000000,f0))
			console.log(sync(1000000000,f1))
			console.log(sync(1000000000,f2))
			
            >                               console.log(sync(1000000000,f0))
            
            { rounds: 1000000000, f: [Function: f0], costed: 28632.76893234253 }
            >                               console.log(sync(1000000000,f1))
            
            { rounds: 1000000000, f: [Function: f1], costed: 9588.860234260559 }
            >                               console.log(sync(1000000000,f2))
            
            { rounds: 1000000000, f: [Function: f2], costed: 14136.279594421387 }

LICENSE

  • ISC

Readme

Keywords

none

Package Sidebar

Install

npm i nv-facutil-simple-test

Weekly Downloads

19

Version

0.0.22

License

ISC

Unpacked Size

49.9 kB

Total Files

3

Last publish

Collaborators

  • ihgazni2