Is there anyway to further optimize calculation speed?

I am fairly devastated because I believe I reach a point when thunkable is fundamentally incapable to fulfill my quest.

I am trying to perform a 4-layer nested loop combination that aims to calculate a number. The initial data looked like this:

This is an election simulation app. For each county, each racial group, each candidate, there will be a sentiment for each factor, indicating that person’s support on a particulat front; for each county, each racial group, there will be an importance, indicating how each factor is valued; there are also enthusiasm attached to each county serving as turnout modifier.

The task is simple, I need to multiple sentiment, enthusiasm and importance together, then multiply accordingly with demographic, TLDR, The center line in the loop will be performed 3000 (counties) x 28 (stratifying groups) x 7 (factors) x 3 (candidates) = around 2 million runs. Initially the center line was a math formula, but I replaced it as a mere simple “set X to 0” to test out its speed.

Unfortunately for me, without the need to run 2 million times, simply running a 5 county example JSON file, which only contains 3,333 runs, takes 5.9 seconds. Obviously the speed is device dependent, but I feel like averaging 600 multiplying task/ sec is not optimal for a platform that I continuously paid for around two years and spent countless hours working on this one project.

Please refer to the picture below for seeing my block:

I am very experienced in using thunkable, so I probably have already used all the known tricks on the table. For example, I do know “Set var to var+x” is FASTER than
“Change var by x”, so in this loop I used the latter; I also know that procedural blocks are considerably slower than direct using blocks; and that stored variable is slower than app variable.

I even know that “wait 0 second” is a terrible thing to use in a loop because it will hundred or thousand fold increase the time it takes, as 0 second actually eats up a few milliseconds.

At this point I am dejected and call for help to see if anyone can help me on optimizing my code, although I am generally convinced that this is the edge of power of thunkable, or my hardware. (I am using IPAD MINI 6THgen and IPhone 15 pro for reference.)

I think it’s interesting that you describe the task as “simple.” Perhaps it’s simple for you but what you’re doing is complex and nested loops are always going to come with a time consequence.

Can you do this outside of Thunkable? I wonder if a Javascript would be faster?

I guess I wonder a little why you’re using Thunkable for this. Surely there’s a server-based option that’s better.

1 Like

Simple as in concept is easy to grasp. To be fair I have limited knowledge outside Thunkable and not until recently do I realize I must consider execute speed , not just the block make sense theoretically. Guess I have a lot to learn!

If I was you, I’d offload this to a cloudflare worker or even use a pure function in a html file in the webviewer and execute js

Much much faster in pure js

Thanks for the tip. As I have never used anything outside Thunkable except knowing very briefly of C#, are there some keywords or quick go to I may rely on that you may enlighten me?