r/CodingHelp • u/mrgk21 • Nov 30 '25
[Javascript] How do i improve performance of 9.7M calculations?!
So right now, we are working on a fintech platform and are managing a page which shows the numbers from a purely CPU driven calculation for a set of 2 combinations of tenors. The maximum number of possible combinations are 5^8 ~ 390k and the worst case performance of loading the table data takes around 8-9mins. We have to improve the performance for this logic somehow, and make it future proof as the client wants to load 5^10 ~ 9.7M rows in under 30seconds and have them in the table without any sort of infinite scrolling and keep all the columns sortable.
Our tech stack is a nextjs frontend, nodejs backend and a golang microservice which we usually use for these sort of calculations. Id say 90% of the work is done in golang and then we perform an iterative linear regression on nodejs and send it to the frontend. Even with all of this, the 390k rows has around 107MB json. With this much data, aggrid starts lagging too. My question is how in the living *** do I even approach this...
I have a few ideas, like,
- moving the linear regression to golang
- get a beefier server for golang and implement multithreading (cause its running a single core rn :) )
- golang service is being called with grpc which has significant latency if called so many times. Reduce the grpc latency, by either streaming or increasing the batch size ( its already batching 500 calc requests together )
- reduce the response bundle size and stream it to nextjs
- swap out aggrid for a custom lightweight html and js only table
- Last ditch option, Recalculate at midnight and store it in cache. Although im unsure how redis and nodejs would perform which
readingstreaming GBs worth of data from it
Also there are a few optimizations that already exist...
- db caching to minimize unnecessary calls
- req caching to remove repeated requests
- filtering out error cases which waste calculations
Any and all suggestions are welcome! Please help a brother out
Edit:
- I hear a lot of people mentioning it's a requirement problem, but this page is actually a brute force page for calculating a ton of combinations. It's to tell the brokers what they can expect from a particular security over time
- I do realise that using any sort of standard libraries in the front end for this is gonna fail. I'm thinking I'll go with storing compressed data in indexed db, and having a rolling window of sorts on top of custom virtualization of the table. There would be worker threads to decompress data depending on the user's scroll position. This seems fine to me tbh, what do you guys think
•
u/AutoModerator Nov 30 '25
Thank you for posting on r/CodingHelp!
Please check our Wiki for answers, guides, and FAQs: https://coding-help.vercel.app
Our Wiki is open source - if you would like to contribute, create a pull request via GitHub! https://github.com/DudeThatsErin/CodingHelp
We are accepting moderator applications: https://forms.fillout.com/t/ua41TU57DGus
We also have a Discord server: https://discord.gg/geQEUBm
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
6
u/andrevanduin_ Nov 30 '25
Did you do any performance measurements? It's almost impossible (and incredibly wasteful) to try and solve performance issues without properly measuring first. Measure everything first, find the slowest parts and start by addressing the easy stuff first.
2
u/shipshaper88 Nov 30 '25
It’s possible that this is a suitable use case for gpu compute but whether it is beneficial depends on exactly what your calculations look like. They are extremely good at very numerous independent calculations. Depending on your knowledge it might also require you to learn a whole new skill set.
1
u/SaltCusp Nov 30 '25
What's your data look like? There is a chance you could reduce the size of if if you used a different data type. If it's price data you could use integer cents instead of floating point dollars for example.
If your data is a time series you could store the diffs instead of the values.
In terms of the regression you could probably fit a curve on a sub set of the data, at least for the first few iterations.
Alternatively instead of a sub set you could make a representative set by ordering your data and taking averages from large groups (think 100 or 1000 data points at a time) if appropriate.
Without knowing more it's hard to say exactly what might help but there's a few shots in the dark for you.
1
u/coloredgreyscale Nov 30 '25
9.7 million calculations should be doable in 1s on a 10mhz single core cpu without simd. (yes division and some other operations take more than a cycle)
There seems something very wrong with how the data is stored / queried / forwarded
Also for that amount of data exchanged json likely is the wrong format.
1
u/Wiszcz Nov 30 '25
Why he don't want inifnite scroll? Talk with client about why, not about how.
If aggrid lags - try to load partialy (even from local cache, not server) - user may see no difference if you mange to do that somehow smart (if it's possible).
Also you can try something other than json (probuff? something like that) for transfer - this can greatly reduce size of payload.
Also - 10m for calucations? I think cache is not working as intended. With cache should be much faster. Check why it's taking so long. Or maybe there are some strange things called to many times and you can cache partial calculations/reuse them.
From my experience - if there is this kind of a problem with to many data at once, solution is sometimes changing approach, not local fixes. They stop scaling always at some point. But - sometimes you have no choice/time so do what you can.
1
u/Ronin-s_Spirit Nov 30 '25 edited Nov 30 '25
If you're doing CPU math work it doesn't really matter which language you use, JS backend will have the same performance unless you can somehow SIMD your work (JS don't have that).
Have you even touched chrome devtools? Painting and layout is extremely slow compared to computations, I'd say your problem is the browser struggling to build a 10M row table. I made a filtering (not sorting) script to read all tables and sort the values of all the rows in some specific categories and create filter selectors for all columns. Very dynamic, very individualized, fully automatic, it took like 4 or 5 different loops and a sorting function, made a bunch of garbage
Setsto be GCed and yet scripting only took3mson the frontent, no pre-calculated work, while rendering took300ms(no fancy manual rendering like on a canvas, just native browser work).1
u/C4CampingTime Dec 01 '25
Can you give more information about the data, are the results reused again? You could perhaps create a background service for processing then save that into a data store.
1
u/jcunews1 Advanced Coder Nov 30 '25
Minimize the use of libraries, especially framework libraries. Most libraries' functions are designed to be catch-all solution. They include code which may not be actually needed for your specific tasks.
I'd suggest extracting the functions which you actually needed from the libraries, copy them into your code, remove unnecessary code in them, and optimize them.
1
u/jaypeejay Nov 30 '25
Loading 9.7M rows is ridiculous no matter what the data is. What is their reasoning for wanting that much data loaded into the UI?
1
u/HolidayWallaby Dec 01 '25
Gpu, it probably doesn't even need to be a big one
1
u/Informal_Pace9237 Dec 03 '25 edited Dec 03 '25
I would go with your option #6. Pre calculate and store in Json. Steam it as a json feed if it is feasible. Remove databases and caches from the equation.
Doesn't make sense to calculate the same over and over.
Just looked at your other answers that rendering is an issue. Did you try to render raw html with CSS from a file and test rendering performance?
1
u/itijara Dec 05 '25
> shows the numbers from a purely CPU driven calculation for a set of 2 combinations of tenors.
So, you said that the calculations are the problem, so you need to speed those up. What exactly are these calculations? You said it is from a set of 2 combinations of tensors, can you just use existing optimized linear algebra libraries?
Do you use the mat package? https://pkg.go.dev/gonum.org/v1/gonum/mat
There is also this binding for openBLAS: https://github.com/blast-go/openblas, which is a very fast C library for doing matrix math.
There is probably also a way to GPU accelerate it.