Skip to content

useMemory

Hook that gives you current memory usage

browser
low
test coverage
Last changed: 7 days ago

TIP

This hook uses performance.memory browser api to provide enhanced functionality. Make sure to check for compatibility with different browsers when using this api

Installation

Library
CLI
Manual
typescript
import { useMemory } from '@siberiacancode/reactuse';

Usage

typescript
const { supported, value } = useMemory();
// or
const { value } = useMemory((nextValue) => { console.log(nextValue.usedJSHeapSize); });

Demo

Api

Parameters

NameTypeDefaultNote
callback?(value: Performance['memory']) => void-The callback to execute when the memory usage changes

Returns

UseMemoryReturn

Type declaration

typescript
interface Performance {
    memory: {
      readonly jsHeapSizeLimit: number;
      readonly totalJSHeapSize: number;
      readonly usedJSHeapSize: number;
    };
  }

export interface UseMemoryReturn {
  /** The memory supported status */
  supported: boolean;
  /** The current memory usage */
  value: Performance['memory'];
}

export type UseMemoryCallback = (value: Performance['memory']) => void;

Source

SourceDemo

Contributors

D
debabin
debabin
H
hywax
hywax

Released under the MIT License.