
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
The reusify npm package is designed to create a fast and low overhead object pool. It allows developers to reuse objects instead of creating new ones, which can help in reducing garbage collection pressure and improving performance in Node.js applications.
Object Pooling
This feature allows you to create a pool of reusable objects. The `get` method is used to acquire an object from the pool, and the `release` method is used to return the object to the pool for future reuse.
const reusify = require('reusify')
function MyObject () {
this.next = null
}
const pool = reusify(MyObject)
function acquire () {
const obj = pool.get()
obj.foo = 'bar'
release(obj)
}
function release (obj) {
pool.release(obj)
}
generic-pool is a robust and generic resource pooling library. It is similar to reusify in that it manages a pool of resources that can be reused. However, generic-pool is more feature-rich and supports priority queuing, object creation with factory functions, and more advanced resource validation and management features.
pool2 is another resource pooling library that offers features like deferred resource creation, soft limits on resource creation, and automatic resource validation and eviction. It provides more control over the resource lifecycle compared to reusify, which is focused on simple object reuse.
object-pool is a minimalistic object pooling library. It shares the same basic concept with reusify, aiming to reduce the overhead of object creation by reusing objects. However, object-pool has a different API and may not be as optimized for performance as reusify.
Reuse your objects and functions for maximum speed. This technique will make any function run ~10% faster. You call your functions a lot, and it adds up quickly in hot code paths.
$ node benchmarks/createNoCodeFunction.js
Total time 53133
Total iterations 100000000
Iteration/s 1882069.5236482036
$ node benchmarks/reuseNoCodeFunction.js
Total time 50617
Total iterations 100000000
Iteration/s 1975620.838848608
The above benchmark uses fibonacci to simulate a real high-cpu load. The actual numbers might differ for your use case, but the difference should not.
The benchmark was taken using Node v6.10.0.
This library was extracted from fastparallel.
var reusify = require('reusify')
var fib = require('reusify/benchmarks/fib')
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.
// if the state contains any external object
// do not use delete operator (it is slow)
// prefer set them to null
obj.num = 0
// store an object in the cache
instance.release(obj)
function MyObject () {
// you need to define this property
// so V8 can compile MyObject into an
// hidden class
this.next = null
this.num = 0
var that = this
// this function is never reallocated,
// so it can be optimized by V8
this.func = function () {
if (null) {
// do nothing
} else {
// calculates fibonacci
fib(that.num)
}
}
}
The above example was intended for synchronous code, let's see async:
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)
}
}
Also note how in the above examples, the code, that consumes an istance of MyObject
,
reset the state to initial condition, just before storing it in the cache.
That's needed so that every subsequent request for an instance from the cache,
could get a clean instance.
It is faster because V8 doesn't have to collect all the functions you create. On a short-lived benchmark, it is as fast as creating the nested function, but on a longer time frame it creates less pressure on the garbage collector.
If you want to see some complex example, checkout middie and steed.
Thanks to Trevor Norris for getting me down the rabbit hole of performance, and thanks to Mathias Buss for suggesting me to share this trick.
MIT
FAQs
Reuse objects and functions with style
The npm package reusify receives a total of 7,510,441 weekly downloads. As such, reusify popularity was classified as popular.
We found that reusify demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.