Skip to content

Commit

Permalink
Update data via lodash's fp/set
Browse files Browse the repository at this point in the history
Use lodahs's "fp/set" instead of "cloneDeep" to increase performance
for data updates, especially for larger models.

Previously JSON Forms used lodash's "cloneDeep" to create a copy
of the current data and then update the relevant parts. This has the
benefit of being a guaranteed immutable update, however it's an
expensive process and hinders using shallow compare on data
objects which were not touched. We now use "fp/set" as a
compromise: It will create new objects for all elements directly
containing the updated data, while leaving sibling elements
alone. This increases performance a lot while still triggering
updates for the changed objects.
  • Loading branch information
DavidHenri008 committed Mar 29, 2021
1 parent 29595e6 commit 83ec3d8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
19 changes: 10 additions & 9 deletions packages/core/src/reducers/core.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*
The MIT License
Copyright (c) 2017-2019 EclipseSource Munich
https://github.com/eclipsesource/jsonforms
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -23,7 +23,7 @@
THE SOFTWARE.
*/
import cloneDeep from 'lodash/cloneDeep';
import set from 'lodash/set';
import setFp from 'lodash/fp/set';
import get from 'lodash/get';
import filter from 'lodash/filter';
import isEqual from 'lodash/isEqual';
Expand Down Expand Up @@ -270,9 +270,7 @@ export const coreReducer: Reducer<JsonFormsCore, CoreActions> = (
} else if (action.path === '') {
// empty path is ok
const result = action.updater(cloneDeep(state.data));

const errors = sanitizeErrors(state.validator, result);

return {
...state,
data: result,
Expand All @@ -281,9 +279,12 @@ export const coreReducer: Reducer<JsonFormsCore, CoreActions> = (
} else {
const oldData: any = get(state.data, action.path);
const newData = action.updater(cloneDeep(oldData));
const newState: any = set(state.data === undefined ? {} : cloneDeep(state.data), action.path, newData);
const newState: any = setFp(
action.path,
newData,
state.data === undefined ? {} : state.data
);
const errors = sanitizeErrors(state.validator, newState);

return {
...state,
data: newState,
Expand Down
43 changes: 34 additions & 9 deletions packages/core/test/reducers/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,13 +468,18 @@ test('core reducer - update - undefined data should update for given path', t =>
})
);

t.not(before, after);
t.not(before.data, after.data);
t.deepEqual(after, { ...before, data: { foo: 'bar'} });
});

test('core reducer - update - path is undefined state should remain same', t => {
const before: JsonFormsCore = {
data: {
foo: 'bar'
foo: 'bar',
baz: {
bar: 'bar'
}
},
schema: {
type: 'object',
Expand All @@ -493,17 +498,23 @@ test('core reducer - update - path is undefined state should remain same', t =>
const after = coreReducer(
before,
update(undefined, _ => {
return { foo: 'xyz' };
return { foo: 'anything' };
})
);

t.is(before, after);
t.is(before.data, after.data);
t.is(before.data.baz, after.data.baz);
t.deepEqual(before, after);
});

test('core reducer - update - path is null state should remain same', t => {
const before: JsonFormsCore = {
data: {
foo: 'bar'
foo: 'bar',
baz: {
bar:'bar'
}
},
schema: {
type: 'object',
Expand All @@ -522,10 +533,13 @@ test('core reducer - update - path is null state should remain same', t => {
const after = coreReducer(
before,
update(null, _ => {
return { foo: 'xyz' };
return { foo: 'anything' };
})
);

t.is(before, after);
t.is(before.data, after.data);
t.is(before.data.baz, after.data.baz);
t.deepEqual(before, after);
});

Expand All @@ -541,7 +555,10 @@ test('core reducer - update - empty path should update root state', t => {

const before: JsonFormsCore = {
data: {
foo: 'bar'
foo: 'bar',
baz: {
bar:'bar'
}
},
errors: [],
schema,
Expand All @@ -558,6 +575,8 @@ test('core reducer - update - empty path should update root state', t => {
})
);

t.not(before, after);
t.not(before.data, after.data);
t.deepEqual(after, { ...before, data: { foo: 'xyz' } });
});

Expand All @@ -577,7 +596,10 @@ test('core reducer - update - providing a path should update data only belonging
const before: JsonFormsCore = {
data: {
animal: 'Sloth',
color: 'Blue'
color: 'Blue',
baz: {
bar: 'bar'
}
},
errors: [],
schema,
Expand All @@ -594,7 +616,10 @@ test('core reducer - update - providing a path should update data only belonging
})
);

t.deepEqual(after, { ...before, data: { animal: 'Sloth', color: 'Green' } });
t.not(before, after);
t.not(before.data, after.data);
t.is(before.data.baz, after.data.baz);
t.deepEqual(after, { ...before, data: { ...before.data, color: 'Green' } });
});

test('core reducer - update - should update errors', t => {
Expand All @@ -614,7 +639,7 @@ test('core reducer - update - should update errors', t => {
const before: JsonFormsCore = {
data: {
animal: 'Sloth',
color: 'Blue'
color: 'Blue',
},
errors: [],
schema,
Expand All @@ -633,7 +658,7 @@ test('core reducer - update - should update errors', t => {

t.deepEqual(after, {
...before,
data: { animal: 'Sloth', color: 'Yellow' },
data: { ...before.data, color: 'Yellow' },
errors: [
{
dataPath: 'color',
Expand Down

0 comments on commit 83ec3d8

Please sign in to comment.