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

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

What's hot (20)

Setting UIAutomation free with Appium
Setting UIAutomation free with AppiumSetting UIAutomation free with Appium
Setting UIAutomation free with Appium
 
Setting Apple's UI Automation Free with Appium
Setting Apple's UI Automation Free with AppiumSetting Apple's UI Automation Free with Appium
Setting Apple's UI Automation Free with Appium
 
E2E testing Single Page Apps and APIs with Cucumber.js and Puppeteer
E2E testing Single Page Apps and APIs with Cucumber.js and PuppeteerE2E testing Single Page Apps and APIs with Cucumber.js and Puppeteer
E2E testing Single Page Apps and APIs with Cucumber.js and Puppeteer
 
Hacking iOS Simulator: writing your own plugins for Simulator
Hacking iOS Simulator: writing your own plugins for SimulatorHacking iOS Simulator: writing your own plugins for Simulator
Hacking iOS Simulator: writing your own plugins for Simulator
 
React Native: The Development Flow
React Native: The Development FlowReact Native: The Development Flow
React Native: The Development Flow
 
End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...
End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...
End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...
 
JAVA INTRODUCTION
JAVA INTRODUCTIONJAVA INTRODUCTION
JAVA INTRODUCTION
 
Reactive Streams and RxJava2
Reactive Streams and RxJava2Reactive Streams and RxJava2
Reactive Streams and RxJava2
 
C# Security Testing and Debugging
C# Security Testing and DebuggingC# Security Testing and Debugging
C# Security Testing and Debugging
 
iOS Parallel Automation: run faster than fast — Viktar Karanevich — SeleniumC...
iOS Parallel Automation: run faster than fast — Viktar Karanevich — SeleniumC...iOS Parallel Automation: run faster than fast — Viktar Karanevich — SeleniumC...
iOS Parallel Automation: run faster than fast — Viktar Karanevich — SeleniumC...
 
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
 
Seven Versions of One Web Application
Seven Versions of One Web ApplicationSeven Versions of One Web Application
Seven Versions of One Web Application
 
iOS Parallel Automation - Viktar Karanevich - Mobile Test Automation Meetup (...
iOS Parallel Automation - Viktar Karanevich - Mobile Test Automation Meetup (...iOS Parallel Automation - Viktar Karanevich - Mobile Test Automation Meetup (...
iOS Parallel Automation - Viktar Karanevich - Mobile Test Automation Meetup (...
 
Scala for Test Automation
Scala for Test AutomationScala for Test Automation
Scala for Test Automation
 
Apache ANT vs Apache Maven
Apache ANT vs Apache MavenApache ANT vs Apache Maven
Apache ANT vs Apache Maven
 
Baruco 2014 - Rubymotion Workshop
Baruco 2014 - Rubymotion WorkshopBaruco 2014 - Rubymotion Workshop
Baruco 2014 - Rubymotion Workshop
 
JavaOne - The JavaFX Community and Ecosystem
JavaOne - The JavaFX Community and EcosystemJavaOne - The JavaFX Community and Ecosystem
JavaOne - The JavaFX Community and Ecosystem
 
Introduction to Groovy Monkey
Introduction to Groovy MonkeyIntroduction to Groovy Monkey
Introduction to Groovy Monkey
 
Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...
Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...
Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...
 
Introduction to Apache Ant
Introduction to Apache AntIntroduction to Apache Ant
Introduction to Apache Ant
 

Viewers also liked

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

Viewers also liked (6)

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

Similar to Titanium - Making the most of your single thread

Building Hermetic Systems (without Docker)
Building Hermetic Systems (without Docker)Building Hermetic Systems (without Docker)
Building Hermetic Systems (without Docker)
William Farrell
 
Why test with flex unit
Why test with flex unitWhy test with flex unit
Why test with flex unit
michael.labriola
 
Realtime selenium interview questions
Realtime selenium interview questionsRealtime selenium interview questions
Realtime selenium interview questions
Kuldeep Pawar
 
Building scalable applications with angular js
Building scalable applications with angular jsBuilding scalable applications with angular js
Building scalable applications with angular js
Andrew Alpert
 
Debugging Complex Systems - Erlang Factory SF 2015
Debugging Complex Systems - Erlang Factory SF 2015Debugging Complex Systems - Erlang Factory SF 2015
Debugging Complex Systems - Erlang Factory SF 2015
lpgauth
 
Stateful patterns in Azure Functions
Stateful patterns in Azure FunctionsStateful patterns in Azure Functions
Stateful patterns in Azure Functions
Massimo Bonanni
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit test
Eugenio Lentini
 
Java script unit testing
Java script unit testingJava script unit testing
Java script unit testing
Mats Bryntse
 
Develop in ludicrous mode with azure serverless
Develop in ludicrous mode with azure serverlessDevelop in ludicrous mode with azure serverless
Develop in ludicrous mode with azure serverless
Lalit Kale
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha Touch
Mats Bryntse
 
Troubleshooting: The Two Laws - IXIASOFT User Conference 2016
Troubleshooting: The Two Laws - IXIASOFT User Conference 2016Troubleshooting: The Two Laws - IXIASOFT User Conference 2016
Troubleshooting: The Two Laws - IXIASOFT User Conference 2016
IXIASOFT
 
Conf orm - explain
Conf orm - explainConf orm - explain
Conf orm - explain
Louise Grandjonc
 
High Performance JavaScript (CapitolJS 2011)
High Performance JavaScript (CapitolJS 2011)High Performance JavaScript (CapitolJS 2011)
High Performance JavaScript (CapitolJS 2011)Nicholas Zakas
 
PART-3 : Mastering RTOS FreeRTOS and STM32Fx with Debugging
PART-3 : Mastering RTOS FreeRTOS and STM32Fx with DebuggingPART-3 : Mastering RTOS FreeRTOS and STM32Fx with Debugging
PART-3 : Mastering RTOS FreeRTOS and STM32Fx with Debugging
FastBit Embedded Brain Academy
 
Defcon 22-paul-mcmillan-attacking-the-iot-using-timing-attac
Defcon 22-paul-mcmillan-attacking-the-iot-using-timing-attacDefcon 22-paul-mcmillan-attacking-the-iot-using-timing-attac
Defcon 22-paul-mcmillan-attacking-the-iot-using-timing-attac
Priyanka Aash
 
[Java concurrency]01.thread management
[Java concurrency]01.thread management[Java concurrency]01.thread management
[Java concurrency]01.thread management
xuehan zhu
 
Beyond the RTOS: A Better Way to Design Real-Time Embedded Software
Beyond the RTOS: A Better Way to Design Real-Time Embedded SoftwareBeyond the RTOS: A Better Way to Design Real-Time Embedded Software
Beyond the RTOS: A Better Way to Design Real-Time Embedded Software
Quantum Leaps, LLC
 
When Web Services Go Bad
When Web Services Go BadWhen Web Services Go Bad
When Web Services Go Bad
Steve Loughran
 

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

Building Hermetic Systems (without Docker)
Building Hermetic Systems (without Docker)Building Hermetic Systems (without Docker)
Building Hermetic Systems (without Docker)
 
Java multi thread programming on cmp system
Java multi thread programming on cmp systemJava multi thread programming on cmp system
Java multi thread programming on cmp system
 
Why test with flex unit
Why test with flex unitWhy test with flex unit
Why test with flex unit
 
Realtime selenium interview questions
Realtime selenium interview questionsRealtime selenium interview questions
Realtime selenium interview questions
 
Building scalable applications with angular js
Building scalable applications with angular jsBuilding scalable applications with angular js
Building scalable applications with angular js
 
Debugging Complex Systems - Erlang Factory SF 2015
Debugging Complex Systems - Erlang Factory SF 2015Debugging Complex Systems - Erlang Factory SF 2015
Debugging Complex Systems - Erlang Factory SF 2015
 
Stateful patterns in Azure Functions
Stateful patterns in Azure FunctionsStateful patterns in Azure Functions
Stateful patterns in Azure Functions
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit test
 
Java script unit testing
Java script unit testingJava script unit testing
Java script unit testing
 
Develop in ludicrous mode with azure serverless
Develop in ludicrous mode with azure serverlessDevelop in ludicrous mode with azure serverless
Develop in ludicrous mode with azure serverless
 
Devignition 2011
Devignition 2011Devignition 2011
Devignition 2011
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha Touch
 
Troubleshooting: The Two Laws - IXIASOFT User Conference 2016
Troubleshooting: The Two Laws - IXIASOFT User Conference 2016Troubleshooting: The Two Laws - IXIASOFT User Conference 2016
Troubleshooting: The Two Laws - IXIASOFT User Conference 2016
 
Conf orm - explain
Conf orm - explainConf orm - explain
Conf orm - explain
 
High Performance JavaScript (CapitolJS 2011)
High Performance JavaScript (CapitolJS 2011)High Performance JavaScript (CapitolJS 2011)
High Performance JavaScript (CapitolJS 2011)
 
PART-3 : Mastering RTOS FreeRTOS and STM32Fx with Debugging
PART-3 : Mastering RTOS FreeRTOS and STM32Fx with DebuggingPART-3 : Mastering RTOS FreeRTOS and STM32Fx with Debugging
PART-3 : Mastering RTOS FreeRTOS and STM32Fx with Debugging
 
Defcon 22-paul-mcmillan-attacking-the-iot-using-timing-attac
Defcon 22-paul-mcmillan-attacking-the-iot-using-timing-attacDefcon 22-paul-mcmillan-attacking-the-iot-using-timing-attac
Defcon 22-paul-mcmillan-attacking-the-iot-using-timing-attac
 
[Java concurrency]01.thread management
[Java concurrency]01.thread management[Java concurrency]01.thread management
[Java concurrency]01.thread management
 
Beyond the RTOS: A Better Way to Design Real-Time Embedded Software
Beyond the RTOS: A Better Way to Design Real-Time Embedded SoftwareBeyond the RTOS: A Better Way to Design Real-Time Embedded Software
Beyond the RTOS: A Better Way to Design Real-Time Embedded Software
 
When Web Services Go Bad
When Web Services Go BadWhen Web Services Go Bad
When Web Services Go Bad
 

Recently uploaded

Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 

Recently uploaded (20)

Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 

Titanium - Making the most of your single thread

  • 1. Making the most of your single thread Titanium - Fundamentals
  • 2. 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…
  • 3. Absolutely Nothing. He tries touching it three times, before deciding, after a mere few seconds, that your app has crashed. ! He kills your app.
  • 4. 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.
  • 5. What did you do wrong? • There’s no loading indicator showing • There is no mechanic in place to prevent additional windows from opening
  • 6. 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!
  • 7. Ronald Treur Co-founder Snowciety ! Freelance developer ! 2.5 yrs Titanium experience ! @ronaldtreur ronald@funcracker.net
  • 8. Making the Most of Your Single Thread • Threads & Threading • How it works in Native & Titanium • Javascript call stack example • Underscore - Defer
  • 9. 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.
  • 11. 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
  • 14. How your app works JS Thread #1
  • 15. 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.
  • 16. 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.
  • 18. 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
  • 19. 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
  • 20. 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
  • 21. 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
  • 22. 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
  • 23. 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
  • 24. 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
  • 25. 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
  • 26. 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.
  • 27. setTimeout vs setInterval setTimeout ( function nerf() { ….. setTimeout ( nerf, 10); }, 10); -VSsetInterval ( function() { ….. }, 10);
  • 28. 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.
  • 31. 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.
  • 32. 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);