Skip to content

Commit 23d86b5

Browse files
committed
refactor(data.controller): replace console.time with performance.now
Fixes concurrency bug and potential memory leaks by replacing globally-scoped console.time with performance.now, using the module level isDebug constant.
1 parent 4dc7218 commit 23d86b5

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

apps/public-api/src/controllers/data.controller.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ const { getConnection } = require("@urbackend/common");
55
const { getCompiledModel } = require("@urbackend/common");
66
const {QueryEngine} = require("@urbackend/common");
77
const { validateData, validateUpdateData } = require("@urbackend/common");
8+
const { performance } = require('perf_hooks');
9+
10+
const isDebug = process.env.DEBUG === 'true';
811

912
// Validate MongoDB ObjectId
1013
const isValidId = (id) => mongoose.Types.ObjectId.isValid(id);
1114

1215
// INSERT DATA
1316
module.exports.insertData = async (req, res) => {
1417
try {
15-
console.time("insert data")
18+
let start;
19+
if (isDebug) start = performance.now();
1620
const { collectionName } = req.params;
1721
const project = req.project;
1822

@@ -48,7 +52,7 @@ module.exports.insertData = async (req, res) => {
4852
);
4953
}
5054

51-
console.timeEnd("insert data")
55+
if (isDebug) console.log(`[DEBUG] insert data took ${(performance.now() - start).toFixed(2)}ms`);
5256
res.status(201).json(result);
5357
} catch (err) {
5458
console.error(err);
@@ -59,7 +63,8 @@ module.exports.insertData = async (req, res) => {
5963
// GET ALL DATA
6064
module.exports.getAllData = async (req, res) => {
6165
try {
62-
console.time("getall")
66+
let start;
67+
if (isDebug) start = performance.now();
6368
const { collectionName } = req.params;
6469
const project = req.project;
6570

@@ -75,7 +80,7 @@ module.exports.getAllData = async (req, res) => {
7580
.paginate();
7681

7782
const data = await features.query.lean();
78-
console.timeEnd("getall")
83+
if (isDebug) console.log(`[DEBUG] getall took ${(performance.now() - start).toFixed(2)}ms`);
7984
res.json(data);
8085
} catch (err) {
8186
console.error(err);

0 commit comments

Comments
 (0)