J José Leiva

Optimizing Financial Logic: Shifting Calculations to the Backend

Moving logic from the frontend to the backend is often the first step toward a more secure and performant application. Recently, in the Proyecto-FinanzApp.io project, we transitioned our financial filtering and calculation engine from the client-side to our Express-based API.

The Problem: Client-Side Bloat

Previously, our React frontend was responsible for performing complex array manipulations to filter income and expense records and calculate running totals. This approach presented several challenges:

  • Inconsistent State: Different clients might see different results if calculations were not perfectly synced.
  • Data Exposure: We were fetching entire datasets just to filter them in the browser, unnecessarily increasing payload sizes.
  • Performance: Complex filtering on the main thread would occasionally cause frame drops on lower-end devices.

The Solution: Centralized Calculation Middleware

By migrating these operations to the backend, we ensured that the client only receives the final, calculated data. This also allowed us to leverage database-level filtering using our existing Express middleware patterns.

// Backend logic example for filtering transactions
app.get('/api/transactions/summary', authenticate, (req, res) => {
  const { bankAccountId } = req.query;
  const data = db.transactions.find({ accountId: bankAccountId });

  const total = data.reduce((acc, curr) => acc + curr.amount, 0);
  
  res.json({ total, count: data.length });
});

This refactoring ensures that our Axios requests remain lean. The backend now performs the heavy lifting, using JWT-based authentication to ensure users only ever query their own financial data.

Outcomes

  • Faster Load Times: The frontend receives a pre-calculated summary object rather than a large array of objects.
  • Simplified Components: React components are now focused on UI representation rather than data transformation.
  • Enhanced Security: Sensitive business logic remains hidden on the server, preventing users from manipulating calculation parameters via the console.

Moving calculation logic to the backend isn't just about performance—it's about building a predictable and secure foundation for your application.


Generated with Gitvlg.com

Optimizing Financial Logic: Shifting Calculations to the Backend
J

José Leiva Campos

Author

Share: