Why does PhoneGap seem faster than Titanium?

2019-07-07 10:11发布

I'm trying to measure the execution perfomance of a few cross-platform solutions, among which are: Titanium and PhoneGap.

So here's an example of the Titanium version of my performance tester, it's very simple, but I'm just trying to get a feeling of how fast my code gets executed:

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 get replaced

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

Both are executed on an iPhone 4S. I've run the test numerous times, to eliminate errors.

How in the name of all that is holy can the Titanium version measure ~0.0009 milliseconds per iteration while the PhoneGap version measures ~0.0002 milliseconds per iteration?

Titanium is supposed to compile my javascript code so I expect it to be faster. In this case however it's at least 4 times slower! I'm not an expert on performance testing, but the test I designed should be at least remotely accurate...

Thank you for any tips you can give me.

4条回答
狗以群分
2楼-- · 2019-07-07 10:23

Titanium doesn't convert javascript code to objective-c. Titanium simply uses a javascript to objective-c bridge to communicate with objective-c iOS framework (most importantly User interface objects). More appropriate comparison would be to code titanium's User Interface element (button, label, window, view ), manipulate them and use html,css,image buttons in phonegap.

Phonegap also uses a bridge of it's own and if you know java or objective-c you can make plugins to use native User Interface elements and other Native features of iOS or Android.

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

查看更多
太酷不给撩
3楼-- · 2019-07-07 10:27

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

This makes the App slower. But testing it purely on these things is useless. If you want to do a full suit of testing you need to use the Titanium API too, and compare that to the PhoneGap one.

What you'll notice, as Phonegap does not compile to native code, it will feel different, and visually Titanium will behave faster.

查看更多
Melony?
4楼-- · 2019-07-07 10:39

In your TItanium code, your last line is creating UI objects - this is making a call to Objective-C to create a UITableViewRow and an animation object and then appending it to a UITableView - you are doing 3 operations. I'd be pretty confident that this is what is taking the time. The preferred Ti way of doing this would be create an array of title objects, then using setData on the table at the end.

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

查看更多
We Are One
5楼-- · 2019-07-07 10:42

Oh man, I don't want to start a flame war but I will put in my two cents. First, full disclosure: I'm a contributor to PhoneGap and I've never used Titanium. However I am answering from 15 years of development experience.

I've never found tools that convert code from one language to another to be particularly efficient. Yes, native code should run faster than JavaScript code but I'm willing to bet there are inefficiencies introduced during the translation phase.

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

查看更多
登录 后发表回答