Benchmarks: SetTimeOut vs. Timer vs. CustomTimer

0 Flares Twitter 0 Facebook 0 Google+ 0 Pin It Share 0 Email -- Filament.io 0 Flares ×

For the purpose of a Final State Machine that I’m developing for ND2Dx, I wanted to know which one of those approaches is the fastest and most reliable.

Testing timers is not an easy task as you can’t really stress test them the same way you could test function calls. So here is my approach:

  • each test is given some time to run (4 seconds by default)
  • a number of objects are created and each one of them are counting how many times they get a callback from the timer (function callback for SetTimeOut and custom timer, TIMER event for timer) as well as the time in ms in between each callbacks (given a delay of 20 ms by default)
  • this gives us an average delay for each one of them, that way we can see which one is the most reliable and in which conditions.

My results

I set a delay of 20 ms for each of my tests. So each approach will either call a unique function or fire an event every 20 ms.

first setup:

  • listenersCount: 1000
  • delay: 20 (ms)
  • timer: 4000 (in ms -> 4 seconds)

results:

  • Timer: ~33 ms
  • SetTimeOut: ~33 ms
  • CustomTimer: ~33 ms

Well the first thing to notice is that, even if I set a delay of 20 ms and the frame rate of of the swf at 60, I always get ~33 ms of average delay for all of them. Simple conclusion: my animation is running at 30 frames per seconds (my CustomTimer is using the ENTER_FRAME event)

Second setup:

  • listenersCount: 10000
  • delay: 20 (ms)
  • timer: 4000 (in ms -> 4 seconds)

results:

  • Timer: ~33 ms
  • CustomTimer: ~33 ms
  • SetTimeOut: ~234 ms

I raised the number of listeners to see what impact it has on each one of them: While Timer and CustomTimer still stay around 33 ms of average delay, SetTimeOut is clearly getting behind with 234 ms. That’s a big increase: SetTimeOut doesn’t seem to be made for resources intensive applications.

Third setup:

  • listenersCount: 50000
  • delay: 20 (ms)
  • timer: 4000 (in ms -> 4 seconds)

I pushed the test even further with 50000 listeners and we are now starting to see a clear difference:

  • CustomTimer still stays around 33 ms of average delay
  • Timer is now around 178 ms
  • and SetTimeOut is way much further with 1238 ms…

Conclusion

Well again, build in solutions seem to be less powerful than custom ones coded in AS3 (see also benchmark comparing arrays, objects, dictionary and custom lookuplist class)
We need to take into account that the CustomTimer is calling a function directly using () and not using the apply() method. Having arguments to pass will have an impact on performances. But as the Timer doesn’t allow to pass arguments, I preferred to keep everyone at the same level.

Here is the benchmark for you to test, and the source code for all currently available benchmarks is available at the end of this article.

Get Adobe Flash player

And the complete source code of all currently available benchmarks:

box_zipBenchmarks Source Code 27/02/2014

0 Flares Twitter 0 Facebook 0 Google+ 0 Pin It Share 0 Email -- Filament.io 0 Flares ×

5 Comments

  1. Ben

    Timer (10000 listeners, 20 delay in ms): 24.937101956965087
    SetTimeOut (10000 listeners, 20 delay in ms): 129.9785
    Custom Timer (10000 listeners, 20 delay in ms): 288.1875

    Custom Timer (50000 listeners, 20 delay in ms): 33.049586776859506
    Timer (50000 listeners, 20 delay in ms): 124.4070193939394
    SetTimeOut (50000 listeners, 20 delay in ms): 755.3947666666667

    Windows 7 64 Bit with Opera.
    Strange results.

  2. indeed, did you redo the test with 10000 listeners ? I have 33 ms all the time for CustomTimer and Timer with 10000 listeners.

  3. Nemi

    SetTimeOut (1000 listeners, 20 delay in ms): 33.05219384630662
    Custom Timer (1000 listeners, 20 delay in ms): 33.3
    Timer (1000 listeners, 20 delay in ms): 33.314625

  4. Nemi

    Timer (10000 listeners, 20 delay in ms): 32.332479365079365
    Custom Timer (10000 listeners, 20 delay in ms): 33.291666666666664
    SetTimeOut (10000 listeners, 20 delay in ms): 189.05588571428572

    WIN7 64bit, Chrome

  5. Ben

    I redid the tests today and it looks like the results changed:

    Timer (10000 listeners, 20 delay in ms): 23.613310588235294
    Custom Timer (10000 listeners, 20 delay in ms): 33.325
    SetTimeOut (10000 listeners, 20 delay in ms): 131.23861

    Custom Timer (50000 listeners, 20 delay in ms): 33.705882352941174
    Timer (50000 listeners, 20 delay in ms): 124.54971
    SetTimeOut (50000 listeners, 20 delay in ms): 729.82315

    It’s strange because I tried the benchmark several times yesterday and the results I posted seemed consistent.

Leave a Comment

*