Skip to content

Commit 18cdf8a

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 d0ec7b4 commit 18cdf8a

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
const { randomUUID } = require("crypto");
21
const { sanitize } = require("@urbackend/common");
32
const mongoose = require('mongoose');
43
const { Project } = require("@urbackend/common");
54
const { getConnection } = require("@urbackend/common");
65
const { getCompiledModel } = require("@urbackend/common");
76
const {QueryEngine} = require("@urbackend/common");
87
const { validateData, validateUpdateData } = require("@urbackend/common");
8+
const { performance } = require('perf_hooks');
9+
10+
const isDebug = process.env.DEBUG === 'true';
911

1012
// Validate MongoDB ObjectId
1113
const isValidId = (id) => mongoose.Types.ObjectId.isValid(id);
1214

1315
// INSERT DATA
1416
module.exports.insertData = async (req, res) => {
1517
try {
16-
const isDebug = process.env.DEBUG === 'true';
17-
const timerLabel = isDebug ? `insert data - ${randomUUID()}` : null;
18-
if (isDebug) console.time(timerLabel);
18+
let start;
19+
if (isDebug) start = performance.now();
1920
const { collectionName } = req.params;
2021
const project = req.project;
2122

@@ -51,7 +52,7 @@ module.exports.insertData = async (req, res) => {
5152
);
5253
}
5354

54-
if (isDebug) console.timeEnd(timerLabel);
55+
if (isDebug) console.log(`[DEBUG] insert data took ${(performance.now() - start).toFixed(2)}ms`);
5556
res.status(201).json(result);
5657
} catch (err) {
5758
console.error(err);
@@ -62,9 +63,8 @@ module.exports.insertData = async (req, res) => {
6263
// GET ALL DATA
6364
module.exports.getAllData = async (req, res) => {
6465
try {
65-
const isDebug = process.env.DEBUG === 'true';
66-
const timerLabel = isDebug ? `getall - ${randomUUID()}` : null;
67-
if (isDebug) console.time(timerLabel);
66+
let start;
67+
if (isDebug) start = performance.now();
6868
const { collectionName } = req.params;
6969
const project = req.project;
7070

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

8282
const data = await features.query.lean();
83-
if (isDebug) console.timeEnd(timerLabel);
83+
if (isDebug) console.log(`[DEBUG] getall took ${(performance.now() - start).toFixed(2)}ms`);
8484
res.json(data);
8585
} catch (err) {
8686
console.error(err);

0 commit comments

Comments
 (0)