source: frontend/node_modules/reusify/README.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

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