Skip to content

Instantly share code, notes, and snippets.

@geea-develop
Created November 12, 2019 07:50
Show Gist options
  • Save geea-develop/19f42031d1ff1d8dc00b35a211861948 to your computer and use it in GitHub Desktop.
Save geea-develop/19f42031d1ff1d8dc00b35a211861948 to your computer and use it in GitHub Desktop.
Approximate size of object in JS using TypeScript (in KB)
const typeSizes = {
"undefined": () => 0,
"boolean": () => 4,
"number": () => 8,
"string": item => 2 * item.length,
"object": item => !item ? 0 : Object
.keys(item)
.reduce((total, key) => sizeOf(key) + sizeOf(item[key]) + total, 0)
};
export const sizeOf = value => typeSizes[typeof value](value);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment