Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 3.5KB

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