Add compress function to return object with reduced memory usage #123
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Objects are modified in place, arrays are replaced with an array that
only has exactly the amount of capacity needed.
This is useful in cases where the polygons will be used for a long time.
By default, arrays are reserved with extra capacity that won't be used.
(The empty array starts with a capacity of 16 elements by now,
which is inefficient for decoded points of length 2)
slice() allocates a new array, seemingly with shrunken capacity
according to process.memoryUsage.
This has an optional option to deduplicate identical points,
which may be useful for collections of polygons sharing points as well
as for calling compress multiple times with different objects.
It's only safe for read-only uses, so it is disabled by default.
For example, in node-geo-tz issue 131, I saw this change to memory usage
and decoding time on Linux (time zone polygons for the entire world map). This is useful for long-running processes
that repeatedly use the objects.
Note that if the object is not kept around, there's wouldn't be a reason to call compress.
What are your thoughts about adding an optional boolean to
decode(pbf, compressData = false)
, and callingcompress
ifcompressData === true)
(strict equality to guard against accidentally passing extra parameters from Array.prototype.forEach)?
Closes #122