Skip to content

Instantly share code, notes, and snippets.

@danrspencer
Last active August 15, 2016 14:08
Show Gist options
  • Save danrspencer/471e293ed7ae9acbce639f817c2c654e to your computer and use it in GitHub Desktop.
Save danrspencer/471e293ed7ae9acbce639f817c2c654e to your computer and use it in GitHub Desktop.
Get in memory size of a JavaScript value/object
const typeSizes = {
'boolean': () => 4,
'number': () => 8,
'string': (item) => 2 * item.length,
'object': (item) => Object.keys(item).reduce(
(sum, key) => sum + sizeOf(key) + sizeOf(item[key]), 0
)
};
export default 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