D.js">SD.js d8e79ace03 index commit 3 yıl önce SE.js d8e79ace03 index commit 3 yıl önce SG.js d8e79ace03 index commit 3 yıl önce SH.js d8e79ace03 index commit 3 yıl önce SI.js d8e79ace03 index commit 3 yıl önce SK.js d8e79ace03 index commit 3 yıl önce SL.js d8e79ace03 index commit 3 yıl önce SM.js d8e79ace03 index commit 3 yıl önce SN.js d8e79ace03 index commit 3 yıl önce SO.js d8e79ace03 index commit 3 yıl önce SR.js d8e79ace03 index commit 3 yıl önce ST.js d8e79ace03 index commit 3 yıl önce SV.js d8e79ace03 index commit 3 yıl önce SY.js d8e79ace03 index commit 3 yıl önce SZ.js d8e79ace03 index commit 3 yıl önce TC.js d8e79ace03 index commit 3 yıl önce TD.js d8e79ace03 index commit 3 yıl önce TG.js d8e79ace03 index commit 3 yıl önce TH.js d8e79ace03 index commit 3 yıl önce TJ.js d8e79ace03 index commit 3 yıl önce TK.js d8e79ace03 index commit 3 yıl önce TL.js d8e79ace03 index commit 3 yıl önce TM.js d8e79ace03 index commit 3 yıl önce TN.js d8e79ace03 index commit 3 yıl önce TO.js d8e79ace03 index commit 3 yıl önce TR.js d8e79ace03 index commit 3 yıl önce TT.js d8e79ace03 index commit 3 yıl önce TV.js d8e79ace03 index commit 3 yıl önce TW.js d8e79ace03 index commit 3 yıl önce TZ.js d8e79ace03 index commit 3 yıl önce UA.js d8e79ace03 index commit 3 yıl önce UG.js d8e79ace03 index commit 3 yıl önce US.js d8e79ace03 index commit 3 yıl önce UY.js d8e79ace03 index commit 3 yıl önce UZ.js d8e79ace03 index commit 3 yıl önce VA.js d8e79ace03 index commit 3 yıl önce VC.js d8e79ace03 index commit 3 yıl önce VE.js d8e79ace03 index commit 3 yıl önce VG.js d8e79ace03 index commit 3 yıl önce VI.js d8e79ace03 index commit 3 yıl önce VN.js d8e79ace03 index commit 3 yıl önce VU.js d8e79ace03 index commit 3 yıl önce WF.js d8e79ace03 index commit 3 yıl önce WS.js d8e79ace03 index commit 3 yıl önce YE.js d8e79ace03 index commit 3 yıl önce YT.js d8e79ace03 index commit 3 yıl önce ZA.js d8e79ace03 index commit 3 yıl önce ZM.js d8e79ace03 index commit 3 yıl önce ZW.js d8e79ace03 index commit 3 yıl önce alt-af.js d8e79ace03 index commit 3 yıl önce alt-an.js d8e79ace03 index commit 3 yıl önce alt-as.js d8e79ace03 index commit 3 yıl önce alt-eu.js d8e79ace03 index commit 3 yıl önce alt-na.js d8e79ace03 index commit 3 yıl önce alt-oc.js d8e79ace03 index commit 3 yıl önce alt-sa.js d8e79ace03 index commit 3 yıl önce alt-ww.js d8e79ace03 index commit 3 yıl önce tum/tmt_learning - Gogs: Simplico Git Service

暂无描述

Prach Pongpanich 6f337d0a21 install tailwind alpine daisyui 3 年之前
..
benchmarks 6f337d0a21 install tailwind alpine daisyui 3 年之前
.coveralls.yml 6f337d0a21 install tailwind alpine daisyui 3 年之前
.travis.yml 6f337d0a21 install tailwind alpine daisyui 3 年之前
LICENSE 6f337d0a21 install tailwind alpine daisyui 3 年之前
README.md 6f337d0a21 install tailwind alpine daisyui 3 年之前
package.json 6f337d0a21 install tailwind alpine daisyui 3 年之前
reusify.js 6f337d0a21 install tailwind alpine daisyui 3 年之前
test.js 6f337d0a21 install tailwind alpine daisyui 3 年之前

README.md

reusify

npm version Build Status Coverage Status

Reuse your objects and functions for maximum speed. This technique will make any function run ~10% faster. You call your functions a lot, and it adds up quickly in hot code paths.

$ node benchmarks/createNoCodeFunction.js
Total time 53133
Total iterations 100000000
Iteration/s 1882069.5236482036

$ node benchmarks/reuseNoCodeFunction.js
Total time 50617
Total iterations 100000000
Iteration/s 1975620.838848608

The above benchmark uses fibonacci to simulate a real high-cpu load. The actual numbers might differ for your use case, but the difference should not.

The benchmark was taken using Node v6.10.0.

This library was extracted from fastparallel.

Example

var reusify = require('reusify')
var fib = require('reusify/benchmarks/fib')
var instance = reusify(MyObject)

// get an object from the cache,
// or creates a new one when cache is empty
var obj = instance.get()

// set the state
obj.num = 100
obj.func()

// reset the state.
// if the state contains any external object
// do not use delete operator (it is slow)
// prefer set them to null
obj.num = 0

// store an object in the cache
instance.release(obj)

function MyObject () {
  // you need to define this property
  // so V8 can compile MyObject into an
  // hidden class
  this.next = null
  this.num = 0

  var that = this

  // this function is never reallocated,
  // so it can be optimized by V8
  this.func = function () {
    if (null) {
      // do nothing
    } else {
      // calculates fibonacci
      fib(that.num)
    }
  }
}

The above example was intended for synchronous code, let's see async:

var reusify = require('reusify')
var instance = reusify(MyObject)

for (var i = 0; i < 100; i++) {
  getData(i, console.log)
}

function getData (value, cb) {
  var obj = instance.get()

  obj.value = value
  obj.cb = cb
  obj.run()
}

function MyObject () {
  this.next = null
  this.value = null

  var that = this

  this.run = function () {
    asyncOperation(that.value, that.handle)
  }

  this.handle = function (err, result) {
    that.cb(err, result)
    that.value = null
    that.cb = null
    instance.release(that)
  }
}

Also note how in the above examples, the code, that consumes an istance of MyObject, reset the state to initial condition, just before storing it in the cache. That's needed so that every subsequent request for an instance from the cache, could get a clean instance.

Why

It is faster because V8 doesn't have to collect all the functions you create. On a short-lived benchmark, it is as fast as creating the nested function, but on a longer time frame it creates less pressure on the garbage collector.

Other examples

If you want to see some complex example, checkout middie and steed.

Acknowledgements

Thanks to Trevor Norris for getting me down the rabbit hole of performance, and thanks to Mathias Buss for suggesting me to share this trick.

License

MIT