Rename estimateDirectMemoryUsageOf to estimateShallowMemoryUsageOf

This commit is contained in:
Jarred Sumner
2024-12-16 20:18:04 -08:00
parent aada6f930f
commit 32c1fdf205
4 changed files with 25 additions and 25 deletions

View File

@@ -772,27 +772,27 @@ console.log(obj); // => { foo: "bar" }
Internally, [`structuredClone`](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone) and [`postMessage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage) serialize and deserialize the same way. This exposes the underlying [HTML Structured Clone Algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm) to JavaScript as an ArrayBuffer.
## `estimateDirectMemoryUsageOf` in `bun:jsc`
## `estimateShallowMemoryUsageOf` in `bun:jsc`
The `estimateDirectMemoryUsageOf` function returns a best-effort estimate of the memory usage of an object in bytes, excluding the memory usage of properties or other objects it references. For accurate per-object memory usage, use `Bun.generateHeapSnapshot`.
The `estimateShallowMemoryUsageOf` function returns a best-effort estimate of the memory usage of an object in bytes, excluding the memory usage of properties or other objects it references. For accurate per-object memory usage, use `Bun.generateHeapSnapshot`.
```js
import { estimateDirectMemoryUsageOf } from "bun:jsc";
import { estimateShallowMemoryUsageOf } from "bun:jsc";
const obj = { foo: "bar" };
const usage = estimateDirectMemoryUsageOf(obj);
const usage = estimateShallowMemoryUsageOf(obj);
console.log(usage); // => 16
const buffer = Buffer.alloc(1024 * 1024);
estimateDirectMemoryUsageOf(buffer);
estimateShallowMemoryUsageOf(buffer);
// => 1048624
const req = new Request("https://bun.sh");
estimateDirectMemoryUsageOf(req);
estimateShallowMemoryUsageOf(req);
// => 167
const array = Array(1024).fill({ a: 1 });
// Arrays are usually not stored contiguously in memory, so this will not return a useful value (which isn't a bug).
estimateDirectMemoryUsageOf(array);
estimateShallowMemoryUsageOf(array);
// => 16
```