Accelerate your Thunkable application

Since I encountered a performance problem with a complex application I programmed in Thunkable (especially on older phones), I decided to explore ways to speed up the application by optimizing the code for speed (sometimes at the cost of degrading its readability).

Here is the first part of my research, how to speed up Thunkable apps and optimize code for speed.

PART 1.

I use the blocks shown below for my tests. The content under test is inserted in the middle, between Timer1_Start and Timer1_Stop.

First I checked how much time a million iterations of the empty loop takes, to make sure there is no significant impact on the result.

Thunkable SpeedTest Block

It is (on my phone) 25 ms. Since I use far fewer iterations with these tests, the error is negligible.


  1. APP VARIABLE vs STORED VARIABLE, 100x

APP vs STORED variable

App variable (2 ms) is tremendously faster than stored variable (402 ms).
Conclusion: avoid using stored variable in deeply nested, repeatedly executed block fragments. Use app variable instead, and only store the final result (if you need it) in stored variable.


  1. CHANGE VARIABLE BY +1 vs SET VARIABLE TO VARIABLE +1, 1000x

Surprise! CHANGE BY +1 gets 27 ms, while SET V to VARIABLE +1 is only 8 ms (!!!). That’s more than three times faster. All the places in my code where I used CHANGE BY x, I converted to old-fashioned V = V + x.


  1. FUNCTION vs DIRECT BLOC, 10000x
    function vs direct
    Another surprise. With 10000 iterations only 45 ms was enough to assign empty text to a variable. However, in function it takes an eternity, more than 120000 ms (hm, two minutes…).
    Conclusion: avoid using functions in deeply nested, repeatedly executed program fragments. Use direct błocka instead. Use functions as a grouping of bigger blocks of code, not small, few block operations, that are executed often.

For balance :smile:, let’s also look at examples of three solutions that provide the same processing speed:

  1. REPEAT vs FOR
    these are equally fast - repeat and for loop

  2. STACKED IF/ELSE vs IF/ELSE IF
    these are equally fast - if

  3. Loading (as background, element background or image) PNG, WEBL, JPG (with the same resolution and color depth). It impacts apk size only.


More tricks to speed up Thunkable code and accelerate applications in the next part (PART2), soon.
Greetings for all Thunkers

9 Likes

Awesome detective work here. Thank you!

1 Like

This is really important to know about Thunkable. Stored variables are so slow! Useful but only use them to set values when absolutely needed; otherwise, transfer the value to an app variable throughout your code, as you suggested.

This is fascinating! Thank you so much for sharing these details.

What?! Another valuable observation. This post should be a sticky post.

2 Likes

I hope part 2 will be more interesting. Still nice improvements are possible.

Now I’m 100% satisfied with speed. :smiley:

Hey @tpirowski :wave:
Great tips! :tada: Looking forward to part 2!

1 Like

I was already using the fastest method withouth knowing :sunglasses:

Just read this, look forward part 2 too!