Thanks for your responses, all!
I use it too support a simple (C++) RAII functionally, to reclaim
resources, both external and internal resources. I believe it is good idea.
It allows a resource, even if an exception get thrown be reclaimed. I
simply code in mind that a exception can get thrown almost anywhere. For
example this code is not safe:
var obj = pool.create(); //may be a connection pool
// code here which may throw an exception
pool.release(obj); //may not ever be reached, if the code before thrown an
exception, therefore not safe
The regular exception handling will not be a solution, because of the
asynchronous design of node.js. However, if I use weak references, I can
ignore the call to "release" if I want too. This feature is specially
useful for implementing multiple design patterns, for example flyweight:
//copy of C++ boost.flyweight, but in JavaScript
var obj = flyweight("This string will only exist once in memory, so even if
I create thousands of them, will no memory increase significantly
happen");//may be of any type
console.log(obj); //we can treat the object just like the object we
constructed it with: "string", with the exception it is read-only.
When all references dies will the object be released automatically, and
when we want to create a new instance will the library create a new cheap
reference to the value already in memory. This would never be possible
without weak references. I have actually created a module like this, which
I plans to release to the public some time.
So in conclusion, I have started to use it whenever there I believe it
fits, in multiple places, but I wonder if I need to worry about any
significant performance degradation?
PS. How would weak maps be used to call a custom function on garbage
collection?
What are your thoughts?
Thanks in advance!