Skip to content

Latest commit

 

History

History
53 lines (37 loc) · 1.27 KB

File metadata and controls

53 lines (37 loc) · 1.27 KB

Memory management

Efficient memory management is crucial in SSR to prevent memory leaks and ensure optimal performance.

Avoid Long-Lived References

Ensure that server-side instances of your application do not hold onto references longer than necessary:

Dispose of Timers: Clear any timers (setTimeout, setInterval) once they're no longer needed.

let timer = setTimeout(() => {
  // Task
}, 1000);

// Later in the code
clearTimeout(timer);

Remove Event Listeners: Detach any event listeners added during rendering.

function onEvent() {
  // Handler
}

element.addEventListener('click', onEvent);

// Later in the code
element.removeEventListener('click', onEvent);

Avoid Modifying Global Prototypes

Never override or extend global prototypes (e.g., Array.prototype) as it can lead to memory leaks and unpredictable behavior across different instances.

// Bad Practice
Array.prototype.push = function(item) {
  // Custom push logic
};

Use Weak References

When possible, use weak references to allow garbage collection of unused objects.

const weakMap = new WeakMap();
let obj = {};
weakMap.set(obj, 'Some value');
// obj can be garbage collected when no longer referenced elsewhere