Skip to content

Memory

Reactive Memory Info

๐ŸŽฌ Usage

<script>
    import {memory} from "@sveu/browser"

    const {supported, result} = memory()
</script>

๐Ÿ‘ฉโ€๐Ÿ’ปAPI

๐Ÿ™ˆ Options

Name Description Type Default
interval The interval in seconds to check the memory. number 1
IntervalFnOptions See IntervalFnOptions. IntervalFnOptions {}

โ†ฉ๏ธ Returns

Name Description Type
supported If the browser supports the memory API. boolean
result A readable store with the memory info. Readable<MemoryInfo | undefined>

๐Ÿงช Playground

Source Code ๐Ÿ‘€

Source Code
import { intervalfn, to_readable, to_writable, unstore } from "@sveu/shared"

import { support } from "../support"
import { MemoryInfo, MemoryOptions, PerformanceMemory } from "../utils"

/**
 * Reactive Memory Info.
 *
 * @param options - Options to use:
 * - `interval` - The interval in seconds to check the memory.
 * - `IntervalFnOptions` - Options from `intervalfn`.
 *
 * @returns
 * - `supported` - If the browser supports the `memory` API.
 * - `result` - A readable store with the memory info.
 *
 */
export function memory(options: MemoryOptions = {}) {
    const _memory = to_writable<MemoryInfo | undefined>(undefined)

    const supported = support("memory", "performance")

    if (unstore(supported)) {
        const { interval = 1 } = options

        intervalfn(
            () => {
                _memory.set({
                    jsHeapSizeLimit: (performance as PerformanceMemory).memory
                        .jsHeapSizeLimit,
                    totalJSHeapSize: (performance as PerformanceMemory).memory
                        .totalJSHeapSize,
                    usedJSHeapSize: (performance as PerformanceMemory).memory
                        .usedJSHeapSize,
                })
            },
            interval,
            {
                immediate: options.immediate,
                immediate_callback: options.immediate_callback,
            }
        )
    }

    return { supported, result: to_readable(_memory) }
}

Last update: 2023-03-09