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?
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
.