views:

355

answers:

4

I've been researching writing an app for iPhone. I really like the look of PhoneGap which basically allows you to contain a webpage in an app. My skills are primarially in HTML/Javascript so this tool allows me to make the most of my skills without having to spend many hours learning how to write an app natively for the iPhone.

I've been doing some tests on my iPhone for Javascript, and some seemingly simple examples run painfully slow. Really slow. This unfortunatly is a big problem for my task!

Any work arounds? If I want to do anything interesting am I going to have to write a 'proper' app?

An explanation on why Apple have seemingly created such a bad implementation of Javascript would be interesting as well (possibly to make more money? Less web apps = more apps in the store?)

References

http://ajaxian.com/archives/ipad-javascript-shockingly-slow

+3  A: 

I don't think Apple has created any special implementation of Javascript for Mobile Safari. Probably it's the same as or very similar to the desktop Safari.

These devices are small and have strict power constraints, so the CPU is slow.

unbeli
A: 

Actually, I think Apple has a vested interest in keeping javascript out of the Iphone as much as possible.. they seem to want to regulate things through their appstore by requiring applications that run natively.. I'm curious if javascript is also slow on Android phones, (I've never used one before).. if its not then I think it is a bit strange that the Iphone would be slow with javascript, at any rate, they are already losing market share and will have to address the issue at some point I am sure, I think people are catching on to Apple's games and idiocy in trying to micromanage everything now that more legitimate alternatives are coming out in the mobile device space.

Rick
Absolute bollocks. The processor is slow. JavaScript is interpreted. Apple makes JavaScript faster on the iPhone with each release (see http://daringfireball.net/2008/07/webkit_performance_iphone for 2.0 and http://waynepan.com/2009/03/24/iphone-30-javascript-performance/ for 3.0). As I understand it, Android 2.2 has better JavaScript performance (http://arstechnica.com/gadgets/news/2010/07/android-22-demolishes-ios4-in-javascript-benchmarks.ars), but you also can’t buy any phones that run 2.2 any more.
Paul D. Waite
That is a load of crap. Nothing to do with the fact that mobile processors are the speed of what desktops were 8 years ago, despite more complex graphics with openGL and more networking being utilised. Android uses almost the same implementation of Safari called the Gecko engine, it is extremely fast compared to others and they are a a large contributor to the open source project, all be it somewhat annoying. From what I have seen of Googles attempts and now Apples attempts to make Gecko faster is to render items before they are ready and imo failing the Acid 3 test
Rudiger
@Rudiger Gecko has absolutely nothing whatsoever to do with Apple, Google or Safari or Android - I assume you meant to say "WebKit". Also, your last line about "rendering things before they are ready" makes no logical sense.@Paul Javascript is generally JIT compiled these days after being interpreted - I'm not sure if iPhone/iPad/Android do this yet, but Opera Mobile on WinMobi, Symbian does JIT compile JS - perhaps iPad JS is so slow because it's not JIT. I don't know.
lucideer
Gecko is the engine behind WebKit, yeah it isn't done by Apple but they are the major contributor to WebKit which is used by Safari, Chrome and other desktop and mobile browsers. rendering things before they are ready refer to the fact that elements that shouldn't be displayed are in the acid 3 test for a brief period. As the acid 3 test states the animation should be smooth and things appearing / disappearing isn't smooth imo. hence rendering an item before it should be rendered.
Rudiger
@Rudiger: [Gecko is the engine behind Firefox](http://en.wikipedia.org/wiki/WebKit). [WebKit is the engine behind Safari](http://en.wikipedia.org/wiki/WebKit). Gecko and WebKit don’t have anything to do with each other, except for the fact that they’re both rendering engines.
Paul D. Waite
@lucideer: Yeah — I know Apple have got “SquirrelFish Extreme” for desktop Safari. I think this made it into iOS 3, but I’m not sure. On Android they might use Google’s V8 JavaScript engine, which took the JIT approach to JavaScript first I think. Can’t find any definite references on this through cursory Googling though.
Paul D. Waite
@Paul Actually I found what I was referring to, Nitro or what you wrote SquirrelFish Extreme. Don't know why I thought it was Gecko. Serves me right for posting on Stack Overflow drunk. I wasn't aware of Google using V8 and thought it was a direct branch of WebKit
Rudiger
I do all of my best Stack Overflow answers after the fourth beer. Yeah I'm not sure what Google do on Android, but I'm pretty sure Chrome uses V8 — JavaScript engines are kept separate from rendering engines I think. Chrome also customizes WebKit in at least one other way: they disable support for the @font-face CSS declaration, or at least did last year. Broadly speaking though, WebKit is WebKit.
Paul D. Waite
@Rudiger: aha, turns out iOS doesn't do JIT compilation: http://daringfireball.net/linked/2010/07/22/android-ios-js-benchmarks
Paul D. Waite
@heh interesting. Whats more important, security or performance? I would suspect Apple would be working on improving it, they take performance of Javascript on the desktop pretty seriously, would be surprised if they didn't care about it on the mobile platform
Rudiger
Note also that for animations they've made lots of CoreAnimation features available via CSS, so you can at least do animations performantly on iOS.
Paul D. Waite
+5  A: 

Javascript is not particularly slow, but the DOM is very slow.
I think it is the same as a desktop browser, but magnified.
I would check first all DOM manipulations, if they can't be optimized.

Another option, is to use a templating engine.
The main DOM manipulations are done through innerHTML injection, which is fast even on mobiles.

We've built a mobile version of our web app, and we use PURE (an open source JS lib we created) to render the HTML from JSON data, and it is very responsive.

We went the HTML5 way(not native) but I think generating the HTML could be done the same way when wrapped in PhoneGap.

Mic
+3  A: 

Apparently iOS won't do JIT compilation of JavaScript (unlike Android) due to a security feature: http://daringfireball.net/linked/2010/07/22/android-ios-js-benchmarks

Good point about DOM access being the issue though: I don't know how much these benchmarks test DOM operations.

Paul D. Waite