Skip to content

Commit a2bc4d0

Browse files
committed
feat: Improve merge performances
It appears @fastify/deepmerge is faster than merge from lodash. To measure performances, we first query 1000 docs. Then, we mutate each doc, and make the query again. We measure the whole `extractAndMergeDocument` function. Before: 1626ms After: 278ms
1 parent 9a03690 commit a2bc4d0

3 files changed

Lines changed: 11 additions & 3 deletions

File tree

packages/cozy-client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
},
1616
"dependencies": {
1717
"@cozy/minilog": "1.0.0",
18+
"@fastify/deepmerge": "^2.0.2",
1819
"@types/jest": "^26.0.20",
1920
"@types/lodash": "^4.14.170",
2021
"btoa": "^1.2.1",

packages/cozy-client/src/store/documents.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import get from 'lodash/get'
22
import omit from 'lodash/omit'
3-
import merge from 'lodash/merge'
4-
import logger from '../logger'
53
import fastEqual from 'fast-deep-equal'
4+
import deepmerge from '@fastify/deepmerge'
5+
import logger from '../logger'
66
import { isReceivingData } from './queries'
77
import { MutationTypes } from '../queries/dsl'
88
import { isReceivingMutationResult } from './mutations'
@@ -221,6 +221,7 @@ export const extractAndMergeDocument = (newData, updatedStateWithIncluded) => {
221221
}
222222

223223
let haveDocumentsChanged = false
224+
const deeppmerge = deepmerge()
224225

225226
newDocsMap.forEach((newDoc, key) => {
226227
const id = key
@@ -235,7 +236,8 @@ export const extractAndMergeDocument = (newData, updatedStateWithIncluded) => {
235236
// - some documents might change without a rev change: typically the thumbnails links
236237
// in io.cozy.files, and the io.cozy.settings.instance doc, with additional
237238
// fields added by the stack
238-
mergedState[doctype][id] = merge({}, mergedState[doctype][id], newDoc)
239+
// - some documents don't have any revision
240+
mergedState[doctype][id] = deeppmerge(mergedState[doctype][id], newDoc)
239241
haveDocumentsChanged = true
240242
}
241243
})

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,11 @@
12351235
minimatch "^3.0.4"
12361236
strip-json-comments "^3.1.1"
12371237

1238+
"@fastify/deepmerge@^2.0.2":
1239+
version "2.0.2"
1240+
resolved "https://registry.yarnpkg.com/@fastify/deepmerge/-/deepmerge-2.0.2.tgz#5dcbda2acb266e309b8a1ca92fa48b2125e65fc0"
1241+
integrity sha512-3wuLdX5iiiYeZWP6bQrjqhrcvBIf0NHbQH1Ur1WbHvoiuTYUEItgygea3zs8aHpiitn0lOB8gX20u1qO+FDm7Q==
1242+
12381243
"@humanwhocodes/config-array@^0.9.2":
12391244
version "0.9.5"
12401245
resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.9.5.tgz#2cbaf9a89460da24b5ca6531b8bbfc23e1df50c7"

0 commit comments

Comments
 (0)