reusify
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Reusify is an efficient and potent NPM package for JavaScript, designed to significantly speed up your functions by reusing objects and functions. By leveraging a cache system, Reusify enables you to reuse instances of your objects, reducing the computational load and optimizing your program's speed by approximately 10%. This can be especially beneficial in hot code paths where functions are called frequently.
To use this dynamic tool, you need to install it in your JavaScript project via npm. Then you can require it in your JavaScript files where you want to use it. Below is a simple example of synchronous code with Reusify.
var reusify = require('reusify')
var instance = reusify(MyObject)
// get an object from the cache,
// or creates a new one when cache is empty
var obj = instance.get()
// set the state
obj.num = 100
obj.func()
// reset the state.
obj.num = 0
// store an object in the cache
instance.release(obj)
For dealing with asynchronous code, here's an example of using Reusify to recycle instances of an object:
var reusify = require('reusify')
var instance = reusify(MyObject)
for (var i = 0; i < 100; i++) {
getData(i, console.log)
}
function getData (value, cb) {
var obj = instance.get()
obj.value = value
obj.cb = cb
obj.run()
}
function MyObject () {
this.next = null
this.value = null
var that = this
this.run = function () {
asyncOperation(that.value, that.handle)
}
this.handle = function (err, result) {
that.cb(err, result)
that.value = null
that.cb = null
instance.release(that)
}
}
Unfortunately, there are no dedicated documentation pages available for the Reusify package at a separate location. The primary source of documentation for Reusify is its README directly on the project's GitHub repository page, which is at https://github.com/mcollina/reusify. Complete with examples and detailed explanations, the README provides the necessary information to understand and use Reusify effectively. Keep track of the repository for future updates, bug fixes, or changes in the project's functionality or syntax.