1

I am trying to assign to a variable the value of performance.memory.jsHeapSizeLimit But since it is not supported on Safari, I would like to assign to the same variable the value 0 when performance.memory.jsHeapSizeLimit doesn't work.

I am doing this:

(function () {
if (performance.memory.jsHeapSizeLimit !== undefined) {
var MAXmemoryAvailable = performance.memory.jsHeapSizeLimit; 
} else { 
var MAXmemoryAvailable = 0; }
})();

The problem is that on Safari it doesn't work and I have the error: undefined is not an object (evaluating 'performance.memory.jsHeapSizeLimit')

Any solution?

  • Does this answer your question? [Null-safe property access (and conditional assignment) in ES6/2015](https://stackoverflow.com/questions/32139078/null-safe-property-access-and-conditional-assignment-in-es6-2015) – A.L Jul 28 '23 at 14:47

1 Answers1

1

You can use optional chaining by adding ?:

The optional chaining (?.) operator accesses an object's property or calls a function. If the object accessed or function called using this operator is undefined or null, the expression short circuits and evaluates to undefined instead of throwing an error.

Example:

(function () {
    if (performance?.memory?.jsHeapSizeLimit !== undefined) {
        var MAXmemoryAvailable = performance.memory.jsHeapSizeLimit; 
    } else { 
        var MAXmemoryAvailable = 0;
    }

    console.log(MAXmemoryAvailable);
})();

With Firefox, which doesn't support performance.memory (and consequently performance.memory.jsHeapSizeLimit), it shows 0.

A.L
  • 10,259
  • 10
  • 67
  • 98