Skip to content

Instantly share code, notes, and snippets.

@idettman
Created January 14, 2020 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idettman/10c7d2e9294e5c78007526034a51a04f to your computer and use it in GitHub Desktop.
Save idettman/10c7d2e9294e5c78007526034a51a04f to your computer and use it in GitHub Desktop.
Expensive Keys
function stringSize(str) {
// JavaScript strings are unicode UTF-16 up to 2 bytes per character
return str.length * 2;
}
function objectSize(obj) {
return stringSize(JSON.stringify(obj));
}
var value = function value(key) {
return function (object) {
return object[key];
};
};
var pickValue = function (key, items) {
return items.map(value(key));
};
function keySize(items, key) {
return stringSize(key) * items.length + objectSize(pickValue(key, items));
}
function zip(keys, values) {
var result = {};
keys.forEach(function (key, index) {
result[key] = values[index];
});
return result;
}
function toMB(bytes) {
return bytes / 1024 / 1024;
}
function toSizeMB(size) {
return toMB(size).toFixed(2) + ' MB';
}
function valuesInMB(obj) {
var result = {};
Object.keys(obj).forEach(function (key) {
var val = obj[key];
if (typeof val === 'number') {
result[key] = toSizeMB(obj[key]);
}
});
return result;
}
function propertySizes(keys, items) {
if (arguments.length === 1) {
items = keys;
if (!Array.isArray(items) && typeof items === 'object') {
items = [items];
}
if (!items.length) {
return {};
}
keys = Object.keys(items[0]);
}
var keyInItemsSize = keySize.bind(null, items);
var sizes = keys.map(keyInItemsSize);
var result = zip(keys, sizes);
result.mb = valuesInMB.bind(null, result);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment