Skip to content

Commit

Permalink
Replace arrays with sets for faster lookup.
Browse files Browse the repository at this point in the history
  • Loading branch information
junhaoliao committed Dec 31, 2024
1 parent 13192bd commit 58a434c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion components/log-viewer-webui/server/src/DbManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class DbManager {
}
const status = job[QUERY_JOBS_TABLE_COLUMN_NAMES.STATUS];

if (false === QUERY_JOB_STATUS_WAITING_STATES.includes(status)) {
if (false === QUERY_JOB_STATUS_WAITING_STATES.has(status)) {
if (QUERY_JOB_STATUS.CANCELLED === status) {
throw new Error(`Job ${jobId} was cancelled.`);
} else if (QUERY_JOB_STATUS.SUCCEEDED !== status) {
Expand Down
11 changes: 6 additions & 5 deletions components/log-viewer-webui/server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import app from "./app.js";


type NodeEnv = "development" | "production" | "test";
const KNOWN_NODE_ENVS = new Set<NodeEnv>([
"development",
"production",
"test",
]);

const NODE_ENV_DEFAULT: NodeEnv = "development";

Expand All @@ -33,11 +38,7 @@ const ENV_TO_LOGGER
* @return True if the `value` is a known Node.js environment; otherwise, false.
*/
const isKnownNodeEnv = (value: string): value is NodeEnv => {
return [
"development",
"production",
"test",
].includes(value);
return KNOWN_NODE_ENVS.has(value as NodeEnv);
};

interface EnvVars {
Expand Down
2 changes: 1 addition & 1 deletion components/log-viewer-webui/server/src/routes/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const routes: FastifyPluginAsync = async (app) => {
},
}, async (req, resp) => {
const {extractJobType, logEventIdx, streamId} = req.body;
if (false === EXTRACT_JOB_TYPES.includes(extractJobType)) {
if (false === EXTRACT_JOB_TYPES.has(extractJobType)) {
resp.code(StatusCodes.BAD_REQUEST);
throw new Error(`Invalid extractJobType="${extractJobType}".`);
}
Expand Down
4 changes: 2 additions & 2 deletions components/log-viewer-webui/server/src/typings/DbManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ enum QUERY_JOB_TYPE {
/**
* List of valid extract job types.
*/
const EXTRACT_JOB_TYPES = Object.freeze([
const EXTRACT_JOB_TYPES = new Set([
QUERY_JOB_TYPE.EXTRACT_IR,
QUERY_JOB_TYPE.EXTRACT_JSON,
]);
Expand All @@ -36,7 +36,7 @@ enum QUERY_JOB_STATUS {
/**
* List of states that indicate the job is either pending or in progress.
*/
const QUERY_JOB_STATUS_WAITING_STATES = Object.freeze([
const QUERY_JOB_STATUS_WAITING_STATES = new Set([
QUERY_JOB_STATUS.PENDING,
QUERY_JOB_STATUS.RUNNING,
QUERY_JOB_STATUS.CANCELLING,
Expand Down

0 comments on commit 58a434c

Please sign in to comment.