I've been building an cordova application on Intel XDK in JS. It calls a function using the setInterval
every few milliseconds.
When I play my application on the emulation or in chrome, it works very smoothly. No problems whatsoever. But when I build the app onto an Android Phone (Galaxy S7), it is about 4x as slow.
Is there a reason for why it is so slow on mobile but not on PC? Can it be prevented? Thanks in advance!
var func = function () {
// code code code
}
setInterval(func,1);
On desktop this will fire about every 4 ms, while on mobile this takes about 20-30 ms, and varies super often.
You are assuming that all JavaScript runtimes and processors are equal. Unfortunately, that is not the case. Your mobile device's compute resources are much less capable than your laptop; the runtime in your laptop/desktop has many more available resources, such as a faster and more capable processor, more RAM, etc. Likewise, the JavaScript runtime in your mobile device is running a JavaScript runtime that has been optimized for battery efficiency and has reduced resources compared to the performance optimized runtime that is running on your laptop/desktop, with what can feel like nearly unlimited resources. Even differences in Android versions will have an impact, especially if you compare the JavaScript runtimes in Android 4.x devices to later version devices.
There is no rule that says the JavaScript event loop will be able to handle four milliseconds in all implementations, so you cannot assume that the performance you measure on platform A will be reproducible on platform B. There are real differences between the platforms and real differences in restrictions and design goals that will impact the results.
This page has a nice description of some of the issue that can arise > https://javascript.info/settimeout-setinterval#summary < and this presentation may help in understanding more about the JavaScript event loop > http://2014.jsconf.eu/speakers/philip-roberts-what-the-heck-is-the-event-loop-anyway.html <
When you run your app in the Intel XDK Simulate tab you are running in a desktop Chromium browser. It does not simulate hardware, it is strictly a convenience to debug logic in your code, nothing more.