namespace

unsafe

namespace unsafe

  • buffer: ArrayBufferLike | Uint8Array<ArrayBuffer>
    ): string;

    Cast bytes to a String without copying. This is the fastest way to get a String from a Uint8Array or ArrayBuffer.

    Only use this for ASCII strings. If there are non-ASCII characters, your application may crash or hit confusing bugs such as "foo" !== "foo".

    The input buffer must not be garbage collected. Hold a reference to it for the lifetime of the string.

    buffer: Uint16Array
    ): string;

    Cast bytes to a String without copying. This is the fastest way to get a String from a Uint16Array

    The input must be a UTF-16 encoded string. This API does no validation whatsoever.

    The input buffer must not be garbage collected. Hold a reference to it for the lifetime of the string.

  • level?: 0 | 2 | 1
    ): 0 | 1 | 2;

    Force the garbage collector to run extremely often, especially inside bun:test.

    • 0: default, disable
    • 1: asynchronously call the garbage collector more often
    • 2: synchronously call the garbage collector more often.

    This is a global setting. It's useful for debugging seemingly random crashes.

    BUN_GARBAGE_COLLECTOR_LEVEL environment variable is also supported.

    @param level

    The level to set: 0, 1, or 2

    @returns

    The previous level

  • function memoryFootprint(): undefined | number;

    Per-process memory footprint in bytes: the memory that only this process keeps the machine from reusing.

    Backed by task_info(TASK_VM_INFO).phys_footprint on macOS (the same number process.memoryUsage.rss() reports there), Pss: from /proc/self/smaps_rollup on Linux (shared pages are split between the processes that map them), and PrivateUsage on Windows (this process's commit charge). Returns undefined on platforms with no such accessor; callers should fall back: Bun.unsafe.memoryFootprint() ?? process.memoryUsage.rss().

  • function mimallocDump(): void;

    Dump the mimalloc heap to the console

  • function setJITPolicy(
    scale: number
    ): void;

    Scale JavaScriptCore's JIT tier-up thresholds for the current thread's VM.

    1 is the normal JIT policy. A value > 1 makes the JIT that many times more reluctant to compile, e.g. during a burst of run-once startup code; it stays in effect until the next call. bun build --compile executables can start with a scale baked in (compile.jitPolicy / --compile-jit-policy) and call setJITPolicy(1) once interactive.

    @param scale

    a finite number >= 1