Created
November 12, 2019 07:50
-
-
Save geea-develop/19f42031d1ff1d8dc00b35a211861948 to your computer and use it in GitHub Desktop.
Approximate size of object in JS using TypeScript (in KB)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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