OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Best Practices for Managing Object References in Callbacks (Javascript)

  • Thread starter Thread starter BennoDev
  • Start date Start date
B

BennoDev

Guest
I am working on a TypeScript project and I am trying to determine the best practice for handling object references within (listener) callbacks. Specifically, I am unsure whether to pass the object instance to the callback or to access the object directly within the callback.

Approach 1: Passing Object to Listener callback

Code:
const obj = createObject();

obj.listen((innerObj) => {
    if (innerObj.isSubmitted && innerObj.config.reValidateMode === 'onChange') {
        innerObj.validate();
    }
});

In this approach, I would push the object (as a WeakRef) with the callback into a queue. Upon processing the queue item and calling the callback, I dereference the WeakRef to access the object.

Approach 2: Direct Object Access within the Callback

Code:
const obj = createObject();

obj.listen(async () => {
    if (obj.isSubmitted && obj.config.reValidateMode === 'onChange') {
        obj.validate();
    }
});

In this approach, I would just push the callback into a queue.

Questions​

  • Which approach is generally considered better in terms of performance and memory management?
  • Are there any best practices or scenarios where one approach is preferable over the other?
  • How does the use of WeakRef in object management impact the performance and memory usage in these contexts?

<p>I am working on a <a href="https://github.com/inbeta-group/monorepo/blob/develop/packages/feature-state/src/create-state.ts" rel="nofollow noreferrer">TypeScript project</a> and I am trying to determine the best practice for handling object references within (listener) callbacks. Specifically, I am unsure whether to pass the object instance to the callback or to access the object directly within the callback.</p>
<p>Approach 1: Passing Object to Listener callback</p>
<pre class="lang-js prettyprint-override"><code>const obj = createObject();

obj.listen((innerObj) => {
if (innerObj.isSubmitted && innerObj.config.reValidateMode === 'onChange') {
innerObj.validate();
}
});
</code></pre>
<p>In this approach, I would push the object (as a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakRef" rel="nofollow noreferrer"><code>WeakRef</code></a>) with the callback into a queue. Upon processing the queue item and calling the callback, I dereference the <code>WeakRef</code> to access the object.</p>
<p>Approach 2: Direct Object Access within the Callback</p>
<pre class="lang-js prettyprint-override"><code>const obj = createObject();

obj.listen(async () => {
if (obj.isSubmitted && obj.config.reValidateMode === 'onChange') {
obj.validate();
}
});
</code></pre>
<p>In this approach, I would just push the callback into a queue.</p>
<h3>Questions</h3>
<ul>
<li>Which approach is generally considered better in terms of performance and memory management?</li>
<li>Are there any best practices or scenarios where one approach is preferable over the other?</li>
<li>How does the use of WeakRef in object management impact the performance and memory usage in these contexts?</li>
</ul>
 

Latest posts

Top