Skip to content

Commit 3025a1f

Browse files
committed
chore: profiling is a noop in prod
1 parent b9228fd commit 3025a1f

File tree

7 files changed

+66
-60
lines changed

7 files changed

+66
-60
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"lint": "eslint src test",
3131
"lint:fix": "npm run lint -- --fix",
3232
"format": "prettier --write \"src/**/*.js\" \"test/**/*.js\"",
33-
"postinstall": "npm run build",
33+
"prepare": "npm run clean && npm run build",
3434
"prepublish": "npm run clean && npm run build",
3535
"pre-push": "npm run lint"
3636
},

src/reducers/cacheReducer.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
} from 'lodash';
2121
import { actionTypes } from '../constants';
2222
import { getBaseQueryName } from '../utils/query';
23-
import mark from '../utils/perfmarks';
23+
import mark from '../utils/profiling';
2424

2525
const info = debug('rrf:cache');
2626

@@ -204,7 +204,7 @@ const populateTransducer = (collection, populates) =>
204204
// Notice: by it's nature populate is O(2^n)/exponential.
205205
// In large data sets, every populate will add substantial time.
206206

207-
mark(`populate.${collection}`);
207+
const done = mark(`populate.${collection}`);
208208

209209
// pre-grab collection and remove empty populations
210210
const lookups = (Array.isArray(populates[0]) ? populates : [populates])
@@ -241,7 +241,7 @@ const populateTransducer = (collection, populates) =>
241241
return draft;
242242
}, createDraft(raw));
243243

244-
mark(`populate.${collection}`, true);
244+
done();
245245

246246
return { database: { [collection]: finishDraft(collectionById) } };
247247
});
@@ -537,7 +537,7 @@ export default function cacheReducer(state = {}, action) {
537537
switch (action.type) {
538538
case actionTypes.GET_SUCCESS:
539539
case actionTypes.LISTENER_RESPONSE:
540-
mark(`cache.${actionTypes.LISTENER_RESPONSE}`);
540+
const done = mark(`cache.LISTENER_RESPONSE`);
541541
if (!draft.database) {
542542
set(draft, ['database'], {});
543543
set(draft, ['databaseOverrides'], {});
@@ -563,11 +563,11 @@ export default function cacheReducer(state = {}, action) {
563563
// append docs field to query
564564
updateCollectionQueries(draft, path);
565565

566-
mark(`cache.${actionTypes.LISTENER_RESPONSE}`, true);
566+
done();
567567
return draft;
568568

569569
case actionTypes.UNSET_LISTENER:
570-
mark(`cache.${actionTypes.UNSET_LISTENER}`);
570+
const unsetDone = mark(`cache.UNSET_LISTENER`);
571571
if (draft[key]) {
572572
// all ids for the collection type, except query to be unset
573573
const activeIds = Object.keys(draft).reduce((inUse, dbKey) => {
@@ -592,12 +592,12 @@ export default function cacheReducer(state = {}, action) {
592592
updateCollectionQueries(draft, path);
593593
}
594594

595-
mark(`cache.${actionTypes.UNSET_LISTENER}`, true);
595+
unsetDone();
596596
return draft;
597597

598598
case actionTypes.DOCUMENT_ADDED:
599599
case actionTypes.DOCUMENT_MODIFIED:
600-
mark(`cache.${actionTypes.DOCUMENT_MODIFIED}`);
600+
const addDone = mark(`cache.DOCUMENT_MODIFIED`);
601601
setWith(
602602
draft,
603603
['database', path, action.meta.doc],
@@ -628,7 +628,7 @@ export default function cacheReducer(state = {}, action) {
628628

629629
updateCollectionQueries(draft, path);
630630

631-
mark(`cache.${actionTypes.DOCUMENT_MODIFIED}`, true);
631+
addDone();
632632
return draft;
633633

634634
case actionTypes.DELETE_SUCCESS:
@@ -637,7 +637,7 @@ export default function cacheReducer(state = {}, action) {
637637
}
638638
// eslint-disable-next-line no-fallthrough
639639
case actionTypes.DOCUMENT_REMOVED:
640-
mark(`cache.${actionTypes.DOCUMENT_REMOVED}`);
640+
const removeDone = mark(`cache.DOCUMENT_REMOVED`);
641641
if (draft.databaseOverrides && draft.databaseOverrides[path]) {
642642
unset(draft, ['databaseOverrides', path, action.meta.doc]);
643643
}
@@ -651,7 +651,7 @@ export default function cacheReducer(state = {}, action) {
651651
// reprocess
652652
updateCollectionQueries(draft, path);
653653

654-
mark(`cache.${actionTypes.DOCUMENT_REMOVED}`, true);
654+
removeDone();
655655
return draft;
656656

657657
case actionTypes.OPTIMISTIC_ADDED:
@@ -676,7 +676,7 @@ export default function cacheReducer(state = {}, action) {
676676
case actionTypes.UPDATE_FAILURE:
677677
case actionTypes.SET_FAILURE:
678678
case actionTypes.ADD_FAILURE:
679-
mark(`cache.${actionTypes.MUTATE_FAILURE}`);
679+
const failureDone = mark(`cache.MUTATE_FAILURE`);
680680
// All failures remove overrides
681681
if (action.payload.data || action.payload.args) {
682682
const write = action.payload.data
@@ -704,11 +704,11 @@ export default function cacheReducer(state = {}, action) {
704704
}
705705
}
706706

707-
mark(`cache.${actionTypes.MUTATE_FAILURE}`, true);
707+
failureDone();
708708
return draft;
709709

710710
case actionTypes.MUTATE_START:
711-
mark(`cache.${actionTypes.MUTATE_START}`);
711+
const startDone = mark(`cache.MUTATE_START`);
712712
if (action.payload && action.payload.data) {
713713
const optimisiticUpdates =
714714
translateMutationToOverrides(action, draft.database) || [];
@@ -731,7 +731,7 @@ export default function cacheReducer(state = {}, action) {
731731
});
732732
}
733733

734-
mark(`cache.${actionTypes.MUTATE_START}`, true);
734+
startDone();
735735
return draft;
736736

737737
default:

src/reducers/dataReducer.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { get } from 'lodash';
22
import { setWith } from 'lodash/fp';
33
import { actionTypes } from '../constants';
44
import { pathFromMeta, preserveValuesFromState } from '../utils/reducers';
5-
import mark from '../utils/perfmarks';
5+
import mark from '../utils/profiling';
66

77
const {
88
CLEAR_DATA,
@@ -32,7 +32,7 @@ export default function dataReducer(state = {}, action) {
3232
switch (action.type) {
3333
case GET_SUCCESS:
3434
case LISTENER_RESPONSE:
35-
mark(`data.${LISTENER_RESPONSE}`);
35+
const done = mark(`data.LISTENER_RESPONSE`);
3636
const { meta, payload } = action;
3737
// Return state if payload are invalid
3838
if (!payload || payload.data === undefined) {
@@ -61,7 +61,7 @@ export default function dataReducer(state = {}, action) {
6161
state,
6262
);
6363
}
64-
mark(`data.${LISTENER_RESPONSE}`, true);
64+
done();
6565
// Set data to state (with merge) immutabily (lodash/fp's setWith creates copy)
6666
return setWith(
6767
Object,

src/utils/mutate.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { chunk, cloneDeep, flatten, isFunction, mapValues } from 'lodash';
22
import debug from 'debug';
33
import { firestoreRef } from './query';
4-
import mark from './perfmarks';
4+
import mark from './profiling';
55

66
const info = debug('rrf:mutate');
77

@@ -164,9 +164,9 @@ function write(firebase, operation = {}, writer = null) {
164164
* @returns {Promise}
165165
*/
166166
function writeSingle(firebase, operations) {
167-
mark('mutate.writeSingle');
167+
const done = mark('mutate.writeSingle');
168168
const promise = write(firebase, operations);
169-
mark('mutate.writeSingle', true);
169+
done();
170170
return promise;
171171
}
172172

@@ -178,7 +178,7 @@ const MAX_BATCH_COUNT = 500;
178178
* @returns {Promise}
179179
*/
180180
async function writeInBatch(firebase, operations) {
181-
mark('mutate.writeInBatch');
181+
const done = mark('mutate.writeInBatch');
182182
const committedBatchesPromised = chunk(operations, MAX_BATCH_COUNT).map(
183183
(operationsChunk) => {
184184
const batch = firebase.firestore().batch();
@@ -190,7 +190,7 @@ async function writeInBatch(firebase, operations) {
190190
},
191191
);
192192

193-
mark('mutate.writeInBatch', true);
193+
done();
194194
return Promise.all(committedBatchesPromised).then(flatten);
195195
}
196196

@@ -210,7 +210,7 @@ async function writeInTransaction(firebase, operations) {
210210
return transaction.get(ref);
211211
};
212212

213-
mark('mutate.writeInTransaction:reads');
213+
const done = mark('mutate.writeInTransaction:reads');
214214
const readsPromised = mapValues(operations.reads, (read) => {
215215
if (isDocRead(read)) {
216216
const doc = firestoreRef(firebase, read);
@@ -230,13 +230,13 @@ async function writeInTransaction(firebase, operations) {
230230
.then((docs) => docs.map(serialize));
231231
});
232232

233-
mark('mutate.writeInTransaction:reads', true);
233+
done();
234234
const reads = await promiseAllObject(readsPromised);
235235

236236
const writes = [];
237237

238238
operations.writes.forEach((writeFnc) => {
239-
mark('mutate.writeInTransaction:writes', true);
239+
const complete = mark('mutate.writeInTransaction:writes');
240240
const operation = isFunction(writeFnc) ? writeFnc(reads) : writeFnc;
241241

242242
if (Array.isArray(operation)) {
@@ -246,7 +246,7 @@ async function writeInTransaction(firebase, operations) {
246246
writes.push(write(firebase, operation, transaction));
247247
}
248248

249-
mark('mutate.writeInTransaction:writes', true);
249+
complete();
250250
});
251251

252252
// Firestore Transaction return null.

src/utils/perfmarks.js

-30
This file was deleted.

src/utils/profiling.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { noop } from 'lodash';
2+
3+
// webpack eternal removes
4+
let win = require('perf_hooks');
5+
6+
const isDev =
7+
!process.env.NODE_ENV ||
8+
process.env.NODE_ENV === 'development' ||
9+
process.env.NODE_ENV === 'test';
10+
11+
try {
12+
win = window;
13+
} catch (e) {}
14+
const perf = win && win.performance;
15+
/**
16+
*
17+
* @param {*} marker
18+
* @param {*} isDone
19+
* @returns {Function}
20+
*/
21+
export default function mark(marker) {
22+
if (!isDev || !perf) return noop;
23+
24+
try {
25+
const now = perf.now();
26+
const start = `@rrf/${marker}-${now}`;
27+
perf.mark(start);
28+
return () => {
29+
perf.measure(`@rrf/${marker}`, start);
30+
};
31+
} catch (err) {
32+
// ensure timings never impact the user
33+
return noop;
34+
}
35+
}

webpack.config.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require('path');
22
const TerserPlugin = require('terser-webpack-plugin');
3+
const webpack = require('webpack');
34

45
const libraryName = 'redux-firestore';
56
const isProduction = process.env.NODE_ENV === 'production';
@@ -16,7 +17,7 @@ const config = {
1617
libraryTarget: 'umd',
1718
umdNamedDefine: true,
1819
},
19-
externals: [],
20+
externals: { perf_hooks: 'var {}' },
2021
optimization: {
2122
minimize: isProduction,
2223
minimizer: isProduction ? [new TerserPlugin()] : [],
@@ -30,7 +31,7 @@ const config = {
3031
},
3132
],
3233
},
33-
plugins: [],
34+
plugins: [new webpack.IgnorePlugin(/perf_hooks/)],
3435
};
3536

3637
module.exports = config;

0 commit comments

Comments
 (0)