A lightweight TypeScript library for estimating memory usage by JavaScript objects.
This will always be an approximate value due to the peculiarities of the JavaScript code execution environment as well as the optimizations applied.
- Accurate (as much as possible) memory estimation for JS/TS objects
- Support for all standard JavaScript types
- Support for exotic types such as Weak Map\Set, etc.
- Support for deep objects
- Support for circular dependency of objects
- Extensibility with custom strategies
- Detailed object memory analysis with keys
- Ease of use
- No dependencies
npm install weigh-js
# or
yarn add weigh-js import { Profiler } from 'weigh-js';
const profiler = new Profiler();
const myObject = { /* ... */ };
// Get total memory in bytes
const memoryUsage = profiler.sizeOf(myObject);
// Get detailed report
const report = profiler.computeDetailedSize(myObject);
// Format for display
console.log(`Memory used: ${Profiler.formatSize(memoryUsage)}`); | Type | Support |
|---|---|
| Boolean | ✅ |
| Number | ✅ |
| BigInt | ✅ |
| String | ✅ |
| Array | ✅ |
| Object | ✅ |
| Map | ✅ |
| Set | ✅ |
| Buffer | ✅ |
| Int8Array | ✅ |
| Uint8Array | ✅ |
| Uint8ClampedArray | ✅ |
| Int16Array | ✅ |
| Uint16Array | ✅ |
| Int32Array | ✅ |
| Uint32Array | ✅ |
| Float32Array | ✅ |
| Float64Array | ✅ |
| BigInt64Array | ✅ |
| BigUint64Array | ✅ |
| Weak Map | ❌ |
| Weak Set | ❌ |
| Weak Ref | ✅ |
| Array Buffer | ✅ |
| DataView | ✅ |
| Function | ✅ |
| Symbol | ✅ |
| Date | ✅ |
| RegExp | ✅ |
| Null | ✅ |
| Undefined | ✅ |
// Custom strategy example
class MockStrategy implements ISizeStrategy {
public supports(value: any): boolean {
return value && value.__mockSize !== undefined;
}
public sizeOf(value: any, _profiler: IProfiler): number {
return value.__mockSize;
}
}
const profiler = new Profiler({
strategies: [
new MockStrategy()
]
});WeighJS | MIT License