[6a3a178] | 1 | # reusify
|
---|
| 2 |
|
---|
| 3 | [![npm version][npm-badge]][npm-url]
|
---|
| 4 | [![Build Status][travis-badge]][travis-url]
|
---|
| 5 | [![Coverage Status][coveralls-badge]][coveralls-url]
|
---|
| 6 |
|
---|
| 7 | Reuse your objects and functions for maximum speed. This technique will
|
---|
| 8 | make any function run ~10% faster. You call your functions a
|
---|
| 9 | lot, and it adds up quickly in hot code paths.
|
---|
| 10 |
|
---|
| 11 | ```
|
---|
| 12 | $ node benchmarks/createNoCodeFunction.js
|
---|
| 13 | Total time 53133
|
---|
| 14 | Total iterations 100000000
|
---|
| 15 | Iteration/s 1882069.5236482036
|
---|
| 16 |
|
---|
| 17 | $ node benchmarks/reuseNoCodeFunction.js
|
---|
| 18 | Total time 50617
|
---|
| 19 | Total iterations 100000000
|
---|
| 20 | Iteration/s 1975620.838848608
|
---|
| 21 | ```
|
---|
| 22 |
|
---|
| 23 | The above benchmark uses fibonacci to simulate a real high-cpu load.
|
---|
| 24 | The actual numbers might differ for your use case, but the difference
|
---|
| 25 | should not.
|
---|
| 26 |
|
---|
| 27 | The benchmark was taken using Node v6.10.0.
|
---|
| 28 |
|
---|
| 29 | This library was extracted from
|
---|
| 30 | [fastparallel](http://npm.im/fastparallel).
|
---|
| 31 |
|
---|
| 32 | ## Example
|
---|
| 33 |
|
---|
| 34 | ```js
|
---|
| 35 | var reusify = require('reusify')
|
---|
| 36 | var fib = require('reusify/benchmarks/fib')
|
---|
| 37 | var instance = reusify(MyObject)
|
---|
| 38 |
|
---|
| 39 | // get an object from the cache,
|
---|
| 40 | // or creates a new one when cache is empty
|
---|
| 41 | var obj = instance.get()
|
---|
| 42 |
|
---|
| 43 | // set the state
|
---|
| 44 | obj.num = 100
|
---|
| 45 | obj.func()
|
---|
| 46 |
|
---|
| 47 | // reset the state.
|
---|
| 48 | // if the state contains any external object
|
---|
| 49 | // do not use delete operator (it is slow)
|
---|
| 50 | // prefer set them to null
|
---|
| 51 | obj.num = 0
|
---|
| 52 |
|
---|
| 53 | // store an object in the cache
|
---|
| 54 | instance.release(obj)
|
---|
| 55 |
|
---|
| 56 | function MyObject () {
|
---|
| 57 | // you need to define this property
|
---|
| 58 | // so V8 can compile MyObject into an
|
---|
| 59 | // hidden class
|
---|
| 60 | this.next = null
|
---|
| 61 | this.num = 0
|
---|
| 62 |
|
---|
| 63 | var that = this
|
---|
| 64 |
|
---|
| 65 | // this function is never reallocated,
|
---|
| 66 | // so it can be optimized by V8
|
---|
| 67 | this.func = function () {
|
---|
| 68 | if (null) {
|
---|
| 69 | // do nothing
|
---|
| 70 | } else {
|
---|
| 71 | // calculates fibonacci
|
---|
| 72 | fib(that.num)
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | ```
|
---|
| 77 |
|
---|
| 78 | The above example was intended for synchronous code, let's see async:
|
---|
| 79 | ```js
|
---|
| 80 | var reusify = require('reusify')
|
---|
| 81 | var instance = reusify(MyObject)
|
---|
| 82 |
|
---|
| 83 | for (var i = 0; i < 100; i++) {
|
---|
| 84 | getData(i, console.log)
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | function getData (value, cb) {
|
---|
| 88 | var obj = instance.get()
|
---|
| 89 |
|
---|
| 90 | obj.value = value
|
---|
| 91 | obj.cb = cb
|
---|
| 92 | obj.run()
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | function MyObject () {
|
---|
| 96 | this.next = null
|
---|
| 97 | this.value = null
|
---|
| 98 |
|
---|
| 99 | var that = this
|
---|
| 100 |
|
---|
| 101 | this.run = function () {
|
---|
| 102 | asyncOperation(that.value, that.handle)
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | this.handle = function (err, result) {
|
---|
| 106 | that.cb(err, result)
|
---|
| 107 | that.value = null
|
---|
| 108 | that.cb = null
|
---|
| 109 | instance.release(that)
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | ```
|
---|
| 113 |
|
---|
| 114 | Also note how in the above examples, the code, that consumes an istance of `MyObject`,
|
---|
| 115 | reset the state to initial condition, just before storing it in the cache.
|
---|
| 116 | That's needed so that every subsequent request for an instance from the cache,
|
---|
| 117 | could get a clean instance.
|
---|
| 118 |
|
---|
| 119 | ## Why
|
---|
| 120 |
|
---|
| 121 | It is faster because V8 doesn't have to collect all the functions you
|
---|
| 122 | create. On a short-lived benchmark, it is as fast as creating the
|
---|
| 123 | nested function, but on a longer time frame it creates less
|
---|
| 124 | pressure on the garbage collector.
|
---|
| 125 |
|
---|
| 126 | ## Other examples
|
---|
| 127 | If you want to see some complex example, checkout [middie](https://github.com/fastify/middie) and [steed](https://github.com/mcollina/steed).
|
---|
| 128 |
|
---|
| 129 | ## Acknowledgements
|
---|
| 130 |
|
---|
| 131 | Thanks to [Trevor Norris](https://github.com/trevnorris) for
|
---|
| 132 | getting me down the rabbit hole of performance, and thanks to [Mathias
|
---|
| 133 | Buss](http://github.com/mafintosh) for suggesting me to share this
|
---|
| 134 | trick.
|
---|
| 135 |
|
---|
| 136 | ## License
|
---|
| 137 |
|
---|
| 138 | MIT
|
---|
| 139 |
|
---|
| 140 | [npm-badge]: https://badge.fury.io/js/reusify.svg
|
---|
| 141 | [npm-url]: https://badge.fury.io/js/reusify
|
---|
| 142 | [travis-badge]: https://api.travis-ci.org/mcollina/reusify.svg
|
---|
| 143 | [travis-url]: https://travis-ci.org/mcollina/reusify
|
---|
| 144 | [coveralls-badge]: https://coveralls.io/repos/mcollina/reusify/badge.svg?branch=master&service=github
|
---|
| 145 | [coveralls-url]: https://coveralls.io/github/mcollina/reusify?branch=master
|
---|