source: trip-planner-front/node_modules/reusify/README.md@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.5 KB
Line 
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
7Reuse your objects and functions for maximum speed. This technique will
8make any function run ~10% faster. You call your functions a
9lot, and it adds up quickly in hot code paths.
10
11```
12$ node benchmarks/createNoCodeFunction.js
13Total time 53133
14Total iterations 100000000
15Iteration/s 1882069.5236482036
16
17$ node benchmarks/reuseNoCodeFunction.js
18Total time 50617
19Total iterations 100000000
20Iteration/s 1975620.838848608
21```
22
23The above benchmark uses fibonacci to simulate a real high-cpu load.
24The actual numbers might differ for your use case, but the difference
25should not.
26
27The benchmark was taken using Node v6.10.0.
28
29This library was extracted from
30[fastparallel](http://npm.im/fastparallel).
31
32## Example
33
34```js
35var reusify = require('reusify')
36var fib = require('reusify/benchmarks/fib')
37var instance = reusify(MyObject)
38
39// get an object from the cache,
40// or creates a new one when cache is empty
41var obj = instance.get()
42
43// set the state
44obj.num = 100
45obj.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
51obj.num = 0
52
53// store an object in the cache
54instance.release(obj)
55
56function 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
78The above example was intended for synchronous code, let's see async:
79```js
80var reusify = require('reusify')
81var instance = reusify(MyObject)
82
83for (var i = 0; i < 100; i++) {
84 getData(i, console.log)
85}
86
87function getData (value, cb) {
88 var obj = instance.get()
89
90 obj.value = value
91 obj.cb = cb
92 obj.run()
93}
94
95function 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
114Also note how in the above examples, the code, that consumes an istance of `MyObject`,
115reset the state to initial condition, just before storing it in the cache.
116That's needed so that every subsequent request for an instance from the cache,
117could get a clean instance.
118
119## Why
120
121It is faster because V8 doesn't have to collect all the functions you
122create. On a short-lived benchmark, it is as fast as creating the
123nested function, but on a longer time frame it creates less
124pressure on the garbage collector.
125
126## Other examples
127If 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
131Thanks to [Trevor Norris](https://github.com/trevnorris) for
132getting me down the rabbit hole of performance, and thanks to [Mathias
133Buss](http://github.com/mafintosh) for suggesting me to share this
134trick.
135
136## License
137
138MIT
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
Note: See TracBrowser for help on using the repository browser.