Need help with object-cull?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

Rich-Harris
131 Stars 2 Forks MIT License 15 Commits 0 Opened issues

Description

Create a copy of an object with just the bits you actually need

Services available

!
?

Need anything else?

Contributors list

object-cull

Create a copy of an object, based on which properties were accessed.

import { prepare, apply } from 'object-cull';

const proxy = prepare({ firstname: 'Terrell', lastname: 'Snider', friends: [ { firstname: 'Rachelle', lastname: 'Knight' }, { firstname: 'Ila', lastname: 'Farrell' }, { firstname: 'Vasquez', lastname: 'Flynn' } ], pets: [ { name: 'Bobo', species: 'Great Dane' } ] });

console.log(proxy.firstname); // Terrell console.log(proxy.friends[0].firstname); // Rachelle

const { kept, culled } = apply(proxy);

console.log(kept); /* { firstname: 'Terrell', friends: [ { firstname: 'Rachelle' } ] } */

console.log(culled); /* [ { path: 'lastname', value: 'Snider' }, { path: 'friends.0.lastname', value: 'Knight' }, { path: 'friends.1', value: { firstname: 'Ila', lastname: 'Farrell' } }, { path: 'friends.2', value: { firstname: 'Vasquez', lastname: 'Flynn' } }, { path: 'pets', value: [ { name: 'Bobo', species: 'Great Dane' } ] } ] */

const unused_bytes = culled.reduce((total, item) => { return total + JSON.stringify(item.value).length; }, 0);

const percent_unused = 100 * unused_bytes / JSON.stringify(proxy).length;

console.log(${percent_unused}% of the data was unused);

Why?

Mostly for server-side rendering. You might not need to serialize all your data to send it to the client.

Prior art

License

MIT

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.