Why does PhoneGap look faster than Titanium?

I am trying to measure the performance of several cross-platform solutions, including Titanium and PhoneGap.

So here is an example of the Titanium version of my performance tester, it's very simple, but I'm just trying to figure out how fast my code is executing:

var looplength;
var start1;
var start2;
var end1;
var end2;
var duration1;
var duration2;
var diff;
var diffpiter;
var power;
var info;

for (power = 0; power < 24; power++) {
  looplength = Math.pow(2, power);

  start1 = new Date().getTime();
  for (iterator = 0; iterator < looplength; iterator++) {a=iterator;b=iterator;}
  end1 = new Date().getTime();

  start2 = new Date().getTime();
  for (iterator = 0; iterator < looplength; iterator++) {a=iterator;}
  end2 = new Date().getTime();

  duration1 = end1 - start1;
  duration2 = end2 - start2;
  diff      = duration1 - duration2;
  diffpiter = diff / looplength;

  info={title:'2^' + power + ' ' + diffpiter};
  tableView.appendRow(Ti.UI.createTableViewRow(info),{animated:true});
}

      

The PhoneGap version is the same except for the last two lines, which are replaced

document.write('2^' + power + ' ' + diffpiter + '<br />');

      

Both are performed on the iPhone 4S. I checked the test several times to resolve the errors.

How in the name of heaven can the Titanium version measure ~0.0009

milliseconds per iteration, while the PhoneGap version measures ~0.0002

milliseconds per iteration?

Titanium has to compile my javascript code, so I expect it to be faster. In this case, however, it is at least 4 times slower! I'm not a performance testing expert, but the test I developed should be at least accurate and accurate ...

Thanks for the advice you can give me.

+3


source to share


4 answers


This is basic JavaScript, and not all JavaScript is compiled to native code. Basically when you are using Titanium API that will be converted to Objective-C or Java code. But to be flexible and dynamic, there is a JavaScript interpreter that is compiled with the application and that basically runs the JavaScript you write.

This makes the application slower. But testing for this is simply useless. If you want to do a full test, you will also need to use the Titanium API, and compare this to PhoneGap.



What you will notice, since Phonegap does not compile native code, it will be different and visually Titanium will behave faster.

+3


source


Titanium does not convert javascript code to objective-c. Titanium just uses javascript for the objective-c bridge to communicate with the iOS objective-c framework (most importantly, the UI objects). A more appropriate comparison would be to code the titanium User Interface element (button, label, window, view), manipulate them and use html, css, image buttons in a phone hesitation.

Phonegap also uses a native bridge and if you know java or objective-c you can make plugins to use native UI elements and other native iOS or Android functionality.



http://zsprawl.com/iOS/2012/05/navigation-bar-with-nativecontrols-in-cordova/

+7


source


Oh man, I don't want to start a fiery war, but I'll bet my two cents. First, full disclosure: I'm the author of PhoneGap and I've never used Titanium. However, I am responsible for 15 years of development experience.

I have never found tools that convert code from one language to another to be particularly efficient. Yes, native code should run faster than JavaScript, but I bet there is inefficiency in the translation phase.

Again, this is just from past experience, using tools that will compile one language into another, this is not a knock on Titanium as it is a great framework.

+2


source


In your TItanium code, your last line is creating UI objects - this is an Objective-C call to create a UITableViewRow and an animation object, and then adding it to the UITableView - you are doing 3 operations. I would be sure this is what takes time. The preferred type of this way would be to create an array of header objects and then use setData on the table at the end.

PhoneGap already created a UIWebView on app load and you are just updating the html in one DOM element, so I expect the UI to be faster.

+1


source







All Articles