Skip to content

Optimize property mapping and array operations for better performance#21

Open
Copilot wants to merge 3 commits intomainfrom
copilot/identify-code-inefficiencies
Open

Optimize property mapping and array operations for better performance#21
Copilot wants to merge 3 commits intomainfrom
copilot/identify-code-inefficiencies

Conversation

Copy link
Contributor

Copilot AI commented Nov 1, 2025

Identified and fixed performance bottlenecks in property mapping and error collection logic.

Changes

Property mapping optimization (src/domain/mapping.ts)

  • Reduced property access operations by 50% using loop-based mapping with single access per property
  • Eliminated array allocations in buildHistoryProps() by building name parts array directly

Before:

if (wb["AI Summary"] !== undefined) props["AI Summary"] = wb["AI Summary"];
if (wb["Health 0-100"] !== undefined) props["Health 0-100"] = wb["Health 0-100"];
// ... 6 more properties

After:

const mappings = [["AI Summary", "AI Summary"], ["Health 0-100", "Health 0-100"], ...] as const;
for (const [key, propName] of mappings) {
  const value = wb[key];
  if (value !== undefined) props[propName] = value;
}

Error collection optimization (src/routes/analyze.ts)

  • Replaced filter().map() chain with single-pass iteration
  • Eliminated intermediate array allocation, reducing complexity from O(2n) to O(n)

Before:

errors: results.filter((r) => r.status === "error").map((r) => r.error || "error")

After:

const errors: string[] = [];
for (const result of results) {
  if (result.status === "error") errors.push(result.error || "error");
}

Removed unnecessary type conversion in HMAC verification path (Buffer.from(String(sig))Buffer.from(sig))

Test coverage

Added 8 unit tests covering property mapping edge cases (partial writebacks, empty objects, name building variations).

Original prompt

Identify and suggest improvements to slow or inefficient code


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits November 1, 2025 23:31
Co-authored-by: stevenschling13 <176538646+stevenschling13@users.noreply.github.com>
Co-authored-by: stevenschling13 <176538646+stevenschling13@users.noreply.github.com>
Copilot AI changed the title [WIP] Identify and suggest improvements for slow code Optimize property mapping and array operations for better performance Nov 1, 2025
Copilot AI requested a review from stevenschling13 November 1, 2025 23:38
@stevenschling13 stevenschling13 marked this pull request as ready for review November 1, 2025 23:41
Copilot AI review requested due to automatic review settings November 1, 2025 23:41
@stevenschling13
Copy link
Owner

@copilot find solution to the error and assure this is ready for deployment

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces test coverage for the mapping functions and applies several performance optimizations and code quality improvements across the codebase. The changes focus on reducing redundant property access, improving type safety, and streamlining error collection logic.

Key changes:

  • Adds comprehensive test suite for mapWritebacksToPhotos and buildHistoryProps functions
  • Refactors mapping functions to use loop-based property mapping instead of repetitive if statements
  • Optimizes error collection in analyze route to use a single iteration
  • Removes unnecessary String() wrapper and improves type safety with as const assertions
  • Updates pnpm lockfile with proper version 9 format

Reviewed Changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 3 comments.

File Description
test/mapping.test.ts Adds new comprehensive test suite covering all mapping function behaviors including edge cases
src/routes/analyze.ts Optimizes error collection logic and improves type safety with const assertions; removes redundant String() wrapper
src/domain/mapping.ts Refactors property mapping to use loop-based approach for better maintainability and performance
pnpm-lock.yaml Updates lockfile from version 5.4 to 9.0 format with complete dependency resolution
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

Comment on lines +18 to +27
const mappings = [
["AI Summary", "AI Summary"],
["Health 0-100", "Health 0-100"],
["VPD OK", "VPD OK"],
["DLI OK", "DLI OK"],
["CO2 OK", "CO2 OK"],
["Trend", "Trend"],
["DLI mol", "DLI mol"],
["VPD kPa", "VPD kPa"],
] as const;
Copy link

Copilot AI Nov 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mappings array contains redundant tuples where the source and target property names are identical. Consider simplifying this to just an array of property names to reduce duplication and improve maintainability. You could map these to themselves in the loop: const simpleProps = ['AI Summary', 'Health 0-100', ...] as const; for (const key of simpleProps) { ... }

Copilot uses AI. Check for mistakes.
Comment on lines +73 to +85
const wbMappings = [
["AI Summary", "AI Summary"],
["Health 0-100", "Health 0-100"],
["DLI mol", "DLI mol"],
["VPD kPa", "VPD kPa"],
["VPD OK", "VPD OK"],
["DLI OK", "DLI OK"],
["CO2 OK", "CO2 OK"],
["Sev", "Sev"],
] as const;

for (const [key, propName] of wbMappings) {
const value = input.wb[key];
Copy link

Copilot AI Nov 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the mappings in mapWritebacksToPhotos, this array contains redundant tuples. The same simplification applies here - use a single array of property names to reduce code duplication.

Suggested change
const wbMappings = [
["AI Summary", "AI Summary"],
["Health 0-100", "Health 0-100"],
["DLI mol", "DLI mol"],
["VPD kPa", "VPD kPa"],
["VPD OK", "VPD OK"],
["DLI OK", "DLI OK"],
["CO2 OK", "CO2 OK"],
["Sev", "Sev"],
] as const;
for (const [key, propName] of wbMappings) {
const value = input.wb[key];
const wbProperties = [
"AI Summary",
"Health 0-100",
"DLI mol",
"VPD kPa",
"VPD OK",
"DLI OK",
"CO2 OK",
"Sev",
] as const;
for (const propName of wbProperties) {
const value = input.wb[propName];

Copilot uses AI. Check for mistakes.
Comment on lines +73 to +79
// Collect errors efficiently during a single iteration
const errors: string[] = [];
for (const result of results) {
if (result.status === "error") {
errors.push(result.error || "error");
}
}
Copy link

Copilot AI Nov 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] While the comment states this collects errors 'efficiently', using filter and map would be more idiomatic and expressive: const errors = results.filter(r => r.status === 'error').map(r => r.error || 'error');. This achieves the same result with clearer intent, though it does iterate twice internally.

Suggested change
// Collect errors efficiently during a single iteration
const errors: string[] = [];
for (const result of results) {
if (result.status === "error") {
errors.push(result.error || "error");
}
}
// Collect errors using idiomatic filter/map
const errors = results
.filter(r => r.status === "error")
.map(r => r.error || "error");

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Copilot AI commented Nov 1, 2025

@copilot find solution to the error and assure this is ready for deployment

The issue was missing node_modules - dependencies weren't installed. After running pnpm install, all checks pass:

✅ Build successful (TypeScript compilation)
✅ All 12 tests passing
✅ Linting passed (ESLint)
✅ Type checking passed
✅ CodeQL security scan: 0 vulnerabilities
✅ dist/ folder generated correctly

Deployment Ready - The code is ready for production deployment.

⚠️ Important: Ensure the HMAC_SECRET environment variable is set in your production environment for webhook security.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants