SlideShare a Scribd company logo
Making the most of your
single thread

Titanium - Fundamentals
Congrats!
!

Your app is in the
hands of the public!
Your users click on buttons,
open windows, and at some
point someone touches a
button that results in…
Absolutely
Nothing.
He tries touching it three
times, before deciding, after
a mere few seconds, that
your app has crashed.

!

He kills your app.
Had he waited
for ‘but’ 5
seconds..
.. he would have noticed
three windows opening in
rapid succession. Three
identical windows, as he
would observe when using
the ‘back’-button.
What did you do wrong?
•

There’s no loading indicator showing

•

There is no mechanic in place to prevent additional
windows from opening
What did you do wrong?
•

There’s no loading indicator showing

•

There is no mechanic in place to prevent additional
windows from opening

•

But more important: You blocked the UI far too long!

You did not make the best use of your single
thread!
Ronald Treur
Co-founder Snowciety

!
Freelance developer

!
2.5 yrs Titanium experience

!
@ronaldtreur

ronald@funcracker.net
Making the Most of Your Single Thread
•

Threads & Threading

•

How it works in Native & Titanium

•

Javascript call stack example

•

Underscore - Defer
Thread
The smallest subset of a
process that could be
independently handled
by the OS.

or
The smallest piece of
code, that could be
executed independently
from the rest of the
code.
Thread
Process

Threads
Threading
•

Multi-threaded: 

- Threads (can) run in parallel, meaning:

- Multiple pieces of code can execute at the same time.
#1
#2

!

#3
#4

!

•

#1

Single-threaded:

- Threads can only run serially, meaning:

- Only one piece of code can execute at a time.
#2

#3
How Native works

Main
Thread

Thread
#1

Thread
#2

Thread
#3

Thread
#4

Thread
#..
How Titanium works

UI

JS

Debug

Main
Thread

Thread
#1

Thread
#2

Thread
#3

Thread
#4
How your app works

JS

Thread
#1
Thread Safe
Javascript is by definition NOT thread safe.
•

If multiple threads would be able to access and change
the same object/variable, a race condition might occur.

•

aka: the outcome of a piece of code would be
unpredictable.
Single Threaded
Javascript is by definition NOT thread safe. Thus it is, by
nature, single threaded.
•

Timers are not part of Javascript, they are part of the
JS engine (Rhino, V8, JavaScriptCore)

•

Timers allow for asynchronous delays, but code
execution remains single threaded.

•

When a timer expires, or an event fires, the resulting
execution is queued.
Call Stack

ms

‘events’

0

foo()

foo()

foo()
0

10

20

ms

30

40

50
Call Stack

ms

‘events’

0
0
0

foo()
setInterval(bar, 10)
setTimeout(nerf, 20)

nerf()

foo()

bar()

bar()

bar()

bar()

bar()

30

40

50

foo()
0

10

20

ms
Call Stack

[click]

foo()

‘events’

0
0
0
6

foo()
setInterval(bar, 10)
setTimeout(nerf, 20)
Button clicked

nerf()

bar()

foo()
0

ms

10

bar()

bar()

bar()

bar()

30

40

50

click
handler
20

ms
Call Stack

ms

In Titanium the queue is
FIFO (first in, first out).

[click]

foo()

0
0
0
6
10

foo()
setInterval(bar, 10)
setTimeout(nerf, 20)
Button clicked
Interval fires

nerf()

bar()

foo()
0

‘events’

10

bar()

bar()

bar()

bar()

40

50

click
bar()
handler
20

ms

30
Call Stack

[click]

foo()

‘events’

0
0
0
6
10
20

foo()
setInterval(bar, 10)
setTimeout(nerf, 20)
Button clicked
Interval fires
Interval & Timer fire

nerf()

bar()

foo()
0

ms

10

bar()

bar()

bar()

bar()

click
bar() nerf()
handler
20

ms

30

40

50
Call Stack

ms

The interval at 20ms is
ignored, an execution is
already scheduled.
[click]

foo()

0
0
0
6
10
20

foo()
setInterval(bar, 10)
setTimeout(nerf, 20)
Button clicked
Interval fires
Interval & Timer fire

nerf()

bar()

foo()
0

‘events’

10

bar()

bar()

bar()

bar()

click
bar() nerf()
handler
20

ms

30

40

50
Call Stack

ms

The interval at 30ms is
queued, since no
execution is scheduled.
[click]

foo()

0
0
0
6
10
20
30

foo()
setInterval(bar, 10)
setTimeout(nerf, 20)
Button clicked
Interval fires
Interval & Timer fire
Interval fires

nerf()

bar()

foo()
0

‘events’

10

bar()

bar()

bar()

bar()

click
bar() nerf() bar()
handler
20

ms

30

40

50
Call Stack

ms

‘events’

nerf()

0
0
0
6
10
20
30
40

foo()
setInterval(bar, 10)
setTimeout(nerf, 20)
Button clicked
Interval fires
Interval & Timer fire
Interval fires
Interval fires

bar()

bar()

Interval at 40ms is
ignored.

[click]

foo()

bar()

foo()
0

10

bar()

bar()

click
bar() nerf() bar()
handler
20

ms

30

40

50
Call Stack

ms

Interval at 50ms is the first
to execute at the correct
time (though 2 executions
were dropped).
[click]

foo()

nerf()

bar()

foo()
0

10

bar()

‘events’

0
0
0
6
10
20
30
40
50

foo()
setInterval(bar, 10)
setTimeout(nerf, 20)
Button clicked
Interval fires
Interval & Timer fire
Interval fires
Interval fires
Interval fires

bar()

bar()

click
bar() nerf() bar()
handler
20

ms

30

40

bar()

bar()
50
Conclusions
•

If code is already executing, events and timers are
queued

•

Timers may not execute exactly when you were
expecting

•

Intervals may not fire as often as you were expecting

•

User interaction is queued as well, which the user
interprets as the UI being unresponsive.
setTimeout vs setInterval
setTimeout ( function nerf() {
	 …..
	 setTimeout ( nerf, 10);
}, 10);
-VSsetInterval ( function() {
	 …..
}, 10);
Underscore - Defer
_.defer(function, [*arguments])
Defers invoking the function until the current call stack has cleared,
similar to using setTimeout with a delay of 0. Useful for performing
expensive computations or HTML rendering in chunks without blocking
the UI thread from updating. If you pass the optional arguments, they
will be forwarded on to the function when it is invoked.
!

_.defer ( function() { alert(‘deferred’); });
!

// Returns from the function before the alert runs.
Live demo

Defer FTW!
https://github.com/RonaldTreur/lp.DeferTests

The above link contains the live demo test cases
Conclusions
•

Try to keep the User experience as responsive as
possible. Use _.defer!

•

Always show loading-indicators when you can’t.

•

Limit the amount of actions everywhere in your app. For
example: Widgitize your button and/or use _.throttle.
Underscore - Throttle
_.throttle(function, wait, [options])
Creates and returns a new, throttled version of the
passed function, that, when invoked repeatedly,
will only actually call the original function at most
once per every wait milliseconds. Useful for ratelimiting events that occur faster than you can keep
up with.
var throttled = _.throttle ( updatePosition, 100);
$(window).scroll(throttled);
The End

Questions?

More Related Content

What's hot (20)

PPTX
Setting UIAutomation free with Appium
Dan Cuellar
 
PPTX
Setting Apple's UI Automation Free with Appium
mobiletestsummit
 
PDF
E2E testing Single Page Apps and APIs with Cucumber.js and Puppeteer
Paul Jensen
 
PDF
Hacking iOS Simulator: writing your own plugins for Simulator
Ahmed Sulaiman
 
PDF
React Native: The Development Flow
Ritesh Kumar
 
PDF
End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...
Paul Jensen
 
PDF
JAVA INTRODUCTION
Prof Ansari
 
PDF
Reactive Streams and RxJava2
Yakov Fain
 
PPTX
C# Security Testing and Debugging
Rich Helton
 
PDF
iOS Parallel Automation: run faster than fast — Viktar Karanevich — SeleniumC...
Badoo
 
PDF
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Ondřej Machulda
 
PDF
Seven Versions of One Web Application
Yakov Fain
 
PDF
iOS Parallel Automation - Viktar Karanevich - Mobile Test Automation Meetup (...
Badoo
 
PPTX
Scala for Test Automation
rthanavarapu
 
PPT
Apache ANT vs Apache Maven
Mudit Gupta
 
PDF
Baruco 2014 - Rubymotion Workshop
Brian Sam-Bodden
 
PDF
JavaOne - The JavaFX Community and Ecosystem
Alexander Casall
 
PPT
Introduction to Groovy Monkey
jervin
 
PDF
Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...
Ondřej Machulda
 
PPT
Introduction to Apache Ant
Muhammad Hafiz Hasan
 
Setting UIAutomation free with Appium
Dan Cuellar
 
Setting Apple's UI Automation Free with Appium
mobiletestsummit
 
E2E testing Single Page Apps and APIs with Cucumber.js and Puppeteer
Paul Jensen
 
Hacking iOS Simulator: writing your own plugins for Simulator
Ahmed Sulaiman
 
React Native: The Development Flow
Ritesh Kumar
 
End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...
Paul Jensen
 
JAVA INTRODUCTION
Prof Ansari
 
Reactive Streams and RxJava2
Yakov Fain
 
C# Security Testing and Debugging
Rich Helton
 
iOS Parallel Automation: run faster than fast — Viktar Karanevich — SeleniumC...
Badoo
 
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Ondřej Machulda
 
Seven Versions of One Web Application
Yakov Fain
 
iOS Parallel Automation - Viktar Karanevich - Mobile Test Automation Meetup (...
Badoo
 
Scala for Test Automation
rthanavarapu
 
Apache ANT vs Apache Maven
Mudit Gupta
 
Baruco 2014 - Rubymotion Workshop
Brian Sam-Bodden
 
JavaOne - The JavaFX Community and Ecosystem
Alexander Casall
 
Introduction to Groovy Monkey
jervin
 
Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...
Ondřej Machulda
 
Introduction to Apache Ant
Muhammad Hafiz Hasan
 

Viewers also liked (6)

PDF
Javascript로 네이티브 iOS, Android앱 만들기 - Titanium
JongEun Lee
 
PDF
Titanium Mobile: flexibility vs. performance
omorandi
 
PDF
10 Golden Rules For Outstanding Titanium Apps
jamessugrue
 
PDF
[Jscamp] Titanium - Javascript를 이용한 크로스 플랫폼 앱 개발
JongEun Lee
 
PDF
Titanium 소개 - 당신이 알고 있는 타이타늄 rev.201310
JongEun Lee
 
PDF
LAP2 iOS and Android Development environment setup
University of Catania
 
Javascript로 네이티브 iOS, Android앱 만들기 - Titanium
JongEun Lee
 
Titanium Mobile: flexibility vs. performance
omorandi
 
10 Golden Rules For Outstanding Titanium Apps
jamessugrue
 
[Jscamp] Titanium - Javascript를 이용한 크로스 플랫폼 앱 개발
JongEun Lee
 
Titanium 소개 - 당신이 알고 있는 타이타늄 rev.201310
JongEun Lee
 
LAP2 iOS and Android Development environment setup
University of Catania
 
Ad

Similar to Titanium - Making the most of your single thread (20)

PPT
Responsive interfaces
Nicholas Zakas
 
PDF
Nicholas' Performance Talk at Google
Nicholas Zakas
 
PPTX
JavaScript Timers, Power Consumption, and Performance
Nicholas Zakas
 
PDF
High Performance JavaScript - jQuery Conference SF Bay Area 2010
Nicholas Zakas
 
PDF
Developing Async Sense
Nemanja Stojanovic
 
PPTX
Threading model in windows store apps
Mirco Vanini
 
PDF
[143]Inside fuse deview 2016
NAVER D2
 
PDF
High Performance JavaScript - WebDirections USA 2010
Nicholas Zakas
 
PPTX
JavaScript Multithread or Single Thread.pptx
RAHITNATH
 
PDF
High Performance JavaScript (Amazon DevCon 2011)
Nicholas Zakas
 
PDF
High Performance JavaScript - Fronteers 2010
Nicholas Zakas
 
PDF
Async Await for Mobile Apps
Craig Dunn
 
PDF
Understanding Asynchronous JavaScript
jnewmanux
 
PPTX
High Performance JavaScript (CapitolJS 2011)
Nicholas Zakas
 
PDF
3 Mobile App Dev Problems - Monospace
Frank Krueger
 
PDF
High Performance JavaScript (YUIConf 2010)
Nicholas Zakas
 
PDF
Should you use HTML5 to build your product? The pros & cons of using current ...
boxuno
 
PPTX
End to-end async and await
vfabro
 
PPTX
All you need to know about the JavaScript event loop
Saša Tatar
 
PDF
Service worker API
Giorgio Natili
 
Responsive interfaces
Nicholas Zakas
 
Nicholas' Performance Talk at Google
Nicholas Zakas
 
JavaScript Timers, Power Consumption, and Performance
Nicholas Zakas
 
High Performance JavaScript - jQuery Conference SF Bay Area 2010
Nicholas Zakas
 
Developing Async Sense
Nemanja Stojanovic
 
Threading model in windows store apps
Mirco Vanini
 
[143]Inside fuse deview 2016
NAVER D2
 
High Performance JavaScript - WebDirections USA 2010
Nicholas Zakas
 
JavaScript Multithread or Single Thread.pptx
RAHITNATH
 
High Performance JavaScript (Amazon DevCon 2011)
Nicholas Zakas
 
High Performance JavaScript - Fronteers 2010
Nicholas Zakas
 
Async Await for Mobile Apps
Craig Dunn
 
Understanding Asynchronous JavaScript
jnewmanux
 
High Performance JavaScript (CapitolJS 2011)
Nicholas Zakas
 
3 Mobile App Dev Problems - Monospace
Frank Krueger
 
High Performance JavaScript (YUIConf 2010)
Nicholas Zakas
 
Should you use HTML5 to build your product? The pros & cons of using current ...
boxuno
 
End to-end async and await
vfabro
 
All you need to know about the JavaScript event loop
Saša Tatar
 
Service worker API
Giorgio Natili
 
Ad

Recently uploaded (20)

PPTX
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PDF
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PDF
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
PPTX
K-Circle-Weekly-Quiz12121212-May2025.pptx
Pankaj Rodey
 
PPTX
Top 10 AI Tools, Like ChatGPT. You Must Learn In 2025
Digilearnings
 
PPTX
Electrophysiology_of_Heart. Electrophysiology studies in Cardiovascular syste...
Rajshri Ghogare
 
PPTX
The Future of Artificial Intelligence Opportunities and Risks Ahead
vaghelajayendra784
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PPTX
Digital Professionalism and Interpersonal Competence
rutvikgediya1
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
Applied-Statistics-1.pptx hardiba zalaaa
hardizala899
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
Tips for Writing the Research Title with Examples
Thelma Villaflores
 
K-Circle-Weekly-Quiz12121212-May2025.pptx
Pankaj Rodey
 
Top 10 AI Tools, Like ChatGPT. You Must Learn In 2025
Digilearnings
 
Electrophysiology_of_Heart. Electrophysiology studies in Cardiovascular syste...
Rajshri Ghogare
 
The Future of Artificial Intelligence Opportunities and Risks Ahead
vaghelajayendra784
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
Digital Professionalism and Interpersonal Competence
rutvikgediya1
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Applied-Statistics-1.pptx hardiba zalaaa
hardizala899
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 

Titanium - Making the most of your single thread