fix: keep -0 and +0 distinct through serialize/deserialize#25
Open
greymoth-jp wants to merge 2 commits into
Open
fix: keep -0 and +0 distinct through serialize/deserialize#25greymoth-jp wants to merge 2 commits into
greymoth-jp wants to merge 2 commits into
Conversation
The serialize memo is a Map, whose keys use SameValueZero, so -0 and +0 share a record when both appear in one value and collapse to a single sign after deserialize. Skip the memo for negative zero so it never shares a record with +0; every other value memoizes as before, so reference sharing, cycles and the existing serialized output are unchanged.
Member
|
I would do that only if TYPE is number and it's falsy otherwise this is a huge performance loss |
Guard the negative-zero check so Object.is runs only when the value is a zero-valued number; non-numbers and nonzero numbers short-circuit before it, so the per-value memo cost is unchanged from before this fix.
Author
|
Gated the -0 check so Object.is only runs on zero-valued numbers now, everything else short-circuits before it, so the per-value cost is gone. Existing tests plus the -0/+0 round-trip pass. Thanks for the review. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
serializememoizes every value it has seen in aMapso shared references and cycles round-trip as one record.Mapkeys use SameValueZero, which treats-0and+0as the same key, so when both appear in one value the second one reuses the first one's record. Afterdeserializethey come back with a single sign:Native
structuredCloneand this module's own defaultclone(which copies values directly instead of going through the memo) both keep the two zeros distinct, soserialize/deserializeis the only path that loses the difference.The fix skips the memo for negative zero only, so
-0can never share a record with+0. Every other value memoizes exactly as before, so reference sharing, cycles and the existing serialized output are unchanged. ASetstill collapses-0/+0into one element, matching native, because that happens in theSetitself and not in the memo.Added a round-trip test alongside the Invalid Date one. The suite passes; reverting the
serialize.jschange makes the new test fail.