Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing useRecords function #113

Merged
merged 2 commits into from
Jun 1, 2023

Conversation

somebee
Copy link
Contributor

@somebee somebee commented May 31, 2023

See issue #112. This PR allows you to set the useRecords option to a function. If useRecords is a function, Packr will call this function for each object, and only use records when the function returns true.

When you have many structured objects combined with many smaller unstructured ones it can make packing smaller and faster, and unpacking substantially faster. For our real-world data we see around 10% size reductions and ~40% faster unpacking!

useRecords:(v=>!!v.id), variableMapSize: true
size: 923kb - pack 8.88069ms - unpack 6.82440ms

useRecords:(v=>!!v.id)
size: 941kb - pack 8.49143ms - unpack 5.41675ms

useRecords: true
size: 1050kb - pack 9.65734ms - unpack 10.21317ms

useRecords: false
size: 2191kb - pack 10.37920ms - unpack 16.75035ms

The only caveat I can think if is that you'll likely want to set mapsAsObjects: true if you simply replace useRecords: true with a function. If useRecords is true, all plain objects will be unpacked as plain objects since they are packed as records. But when you opt out of record packing for certain objects you need to remember that they will then be unpacked as maps now unless you set mapsAsObjects to true.

@kriszyp kriszyp merged commit 30875db into kriszyp:master Jun 1, 2023
@kriszyp
Copy link
Owner

kriszyp commented Jun 1, 2023

Impressive results, this looks good. Thank you for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants