Skip to content

Commit 1396ca8

Browse files
Skn0ttflybayer
andauthored
feat: add in-place deserialize (#320)
* feat: add in-place deserialize * add issue reference * update ci * recommend inplace deserialize * add more explicit inPlace documentation (#323) --------- Co-authored-by: Brandon Bayer <b@bayer.ws>
1 parent 9da87cc commit 1396ca8

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66

77
strategy:
88
matrix:
9-
node: [20.x, 18.x, 16.x]
9+
node: [24.x, 22.x, 20.x, 18.x]
1010

1111
env:
1212
CI: true
@@ -21,14 +21,6 @@ jobs:
2121
with:
2222
node-version: ${{ matrix.node }}
2323

24-
- name: Use cached node_modules
25-
uses: actions/cache@v2
26-
with:
27-
path: node_modules
28-
key: nodeModules-${{ hashFiles('**/yarn.lock') }}
29-
restore-keys: |
30-
nodeModules-
31-
3224
- name: Install dependencies
3325
run: yarn install --frozen-lockfile
3426

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,16 @@ Deserializes the output of Superjson back into your original value.
198198
```js
199199
const { json, meta } = serialize(object);
200200

201-
deserialize({ json, meta });
201+
deserialize({ json, meta }, { inPlace: true });
202202
```
203203

204+
Options
205+
206+
- `inPlace: boolean`
207+
- Default: `false`
208+
- Mutate the input json object in place instead of returning a deep copy
209+
- `inPlace: true` will be much more performant on large objects if it's safe to mutate it
210+
204211
Returns **`your original value`**.
205212

206213
### stringify

src/index.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,16 @@ test('doesnt iterate to keys that dont exist', () => {
12711271
expect(() => SuperJSON.deserialize(res)).toThrowError('index out of bounds');
12721272
});
12731273

1274+
// https://github.com/flightcontrolhq/superjson/issues/319
1275+
test('deserialize in place', () => {
1276+
const serialized = SuperJSON.serialize({ a: new Date() });
1277+
const deserializedCopy = SuperJSON.deserialize(serialized);
1278+
const deserializedInPlace = SuperJSON.deserialize(serialized, { inPlace: true });
1279+
expect(deserializedInPlace).toBe(serialized.json);
1280+
expect(deserializedCopy).not.toBe(serialized.json);
1281+
expect(deserializedCopy).toEqual(deserializedInPlace);
1282+
});
1283+
12741284
test('#310 fixes backwards compat', () => {
12751285
expect(
12761286
SuperJSON.deserialize({

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ export default class SuperJSON {
6060
return res;
6161
}
6262

63-
deserialize<T = unknown>(payload: SuperJSONResult): T {
63+
deserialize<T = unknown>(payload: SuperJSONResult, options?: { inPlace?: boolean }): T {
6464
const { json, meta } = payload;
6565

66-
let result: T = copy(json) as any;
66+
let result: T = options?.inPlace ? json : copy(json) as any;
6767

6868
if (meta?.values) {
6969
result = applyValueAnnotations(result, meta.values, meta.v ?? 0, this);
@@ -85,7 +85,7 @@ export default class SuperJSON {
8585
}
8686

8787
parse<T = unknown>(string: string): T {
88-
return this.deserialize(JSON.parse(string));
88+
return this.deserialize(JSON.parse(string), { inPlace: true });
8989
}
9090

9191
readonly classRegistry = new ClassRegistry();

0 commit comments

Comments
 (0)