{"id":13526509,"url":"https://github.com/mcollina/reusify","last_synced_at":"2025-05-14T11:09:03.835Z","repository":{"id":50819278,"uuid":"42106515","full_name":"mcollina/reusify","owner":"mcollina","description":"Reuse objects and functions with style","archived":false,"fork":false,"pushed_at":"2025-02-25T16:01:06.000Z","size":25,"stargazers_count":178,"open_issues_count":2,"forks_count":18,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-27T05:38:15.734Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mcollina.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["mcollina"]}},"created_at":"2015-09-08T10:45:51.000Z","updated_at":"2025-04-23T17:45:36.000Z","dependencies_parsed_at":"2024-11-08T21:02:39.966Z","dependency_job_id":"90f43a49-c178-4519-abd3-68fe956c29ab","html_url":"https://github.com/mcollina/reusify","commit_stats":{"total_commits":29,"total_committers":9,"mean_commits":"3.2222222222222223","dds":"0.48275862068965514","last_synced_commit":"0d47e6085b64c010c90767dea778ca9fa9ac2fb5"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcollina%2Freusify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcollina%2Freusify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcollina%2Freusify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcollina%2Freusify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mcollina","download_url":"https://codeload.github.com/mcollina/reusify/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251961988,"owners_count":21671963,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T06:01:30.804Z","updated_at":"2025-05-14T11:09:03.811Z","avatar_url":"https://github.com/mcollina.png","language":"JavaScript","readme":"# reusify\n\n[![npm version][npm-badge]][npm-url]\n\nReuse your objects and functions for maximum speed. This technique will\nmake any function run ~10% faster. You call your functions a\nlot, and it adds up quickly in hot code paths.\n\n```\n$ node benchmarks/createNoCodeFunction.js\nTotal time 53133\nTotal iterations 100000000\nIteration/s 1882069.5236482036\n\n$ node benchmarks/reuseNoCodeFunction.js\nTotal time 50617\nTotal iterations 100000000\nIteration/s 1975620.838848608\n```\n\nThe above benchmark uses fibonacci to simulate a real high-cpu load.\nThe actual numbers might differ for your use case, but the difference\nshould not.\n\nThe benchmark was taken using Node v6.10.0.\n\nThis library was extracted from\n[fastparallel](http://npm.im/fastparallel).\n\n## Example\n\n```js\nvar reusify = require('reusify')\nvar fib = require('reusify/benchmarks/fib')\nvar instance = reusify(MyObject)\n\n// get an object from the cache,\n// or creates a new one when cache is empty\nvar obj = instance.get()\n\n// set the state\nobj.num = 100\nobj.func()\n\n// reset the state.\n// if the state contains any external object\n// do not use delete operator (it is slow)\n// prefer set them to null\nobj.num = 0\n\n// store an object in the cache\ninstance.release(obj)\n\nfunction MyObject () {\n  // you need to define this property\n  // so V8 can compile MyObject into an\n  // hidden class\n  this.next = null\n  this.num = 0\n\n  var that = this\n\n  // this function is never reallocated,\n  // so it can be optimized by V8\n  this.func = function () {\n    if (null) {\n      // do nothing\n    } else {\n      // calculates fibonacci\n      fib(that.num)\n    }\n  }\n}\n```\n\nThe above example was intended for synchronous code, let's see async:\n```js\nvar reusify = require('reusify')\nvar instance = reusify(MyObject)\n\nfor (var i = 0; i \u003c 100; i++) {\n  getData(i, console.log)\n}\n\nfunction getData (value, cb) {\n  var obj = instance.get()\n\n  obj.value = value\n  obj.cb = cb\n  obj.run()\n}\n\nfunction MyObject () {\n  this.next = null\n  this.value = null\n\n  var that = this\n\n  this.run = function () {\n    asyncOperation(that.value, that.handle)\n  }\n\n  this.handle = function (err, result) {\n    that.cb(err, result)\n    that.value = null\n    that.cb = null\n    instance.release(that)\n  }\n}\n```\n\nAlso note how in the above examples, the code, that consumes an instance of `MyObject`,\nreset the state to initial condition, just before storing it in the cache.\nThat's needed so that every subsequent request for an instance from the cache,\ncould get a clean instance.\n\n## Why\n\nIt is faster because V8 doesn't have to collect all the functions you\ncreate. On a short-lived benchmark, it is as fast as creating the\nnested function, but on a longer time frame it creates less\npressure on the garbage collector.\n\n## Other examples\nIf you want to see some complex example, checkout [middie](https://github.com/fastify/middie) and [steed](https://github.com/mcollina/steed).\n\n## Acknowledgements\n\nThanks to [Trevor Norris](https://github.com/trevnorris) for\ngetting me down the rabbit hole of performance, and thanks to [Mathias\nBuss](http://github.com/mafintosh) for suggesting me to share this\ntrick.\n\n## License\n\nMIT\n\n[npm-badge]: https://badge.fury.io/js/reusify.svg\n[npm-url]: https://badge.fury.io/js/reusify\n","funding_links":["https://github.com/sponsors/mcollina"],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcollina%2Freusify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmcollina%2Freusify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcollina%2Freusify/lists"}