简体   繁体   中英

Why does Javascript run so slow on iPhone?

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

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.

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.

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.

@Rudiger: Just a thought - A lot of the improvements to desktop computer speed since "8 years ago" have been attained in part through the use of multiple processors. Javascript is single-threaded, and so presumably would not be able to take advantage of such multiple processors. Yes, I know that browsers can take advantage of it, and that putting the other stuff on the other processors can provide more CPU power to the Javascript thread, but I have an app that is mostly raw Javscript internal processing, where the main thing that is going on is search and array manipulation.

So, when comparing desktop power to mobile processor power, for my purposes, maybe the slowdown would not be so bad? I currently run at very acceptable speeds on Safari on a six-year-old notebook computer with a single processor. So I'm thinking that Safari on iPhone or iPad for me might not be that much worse. Do you think this is reasonable?

实际上,我认为Apple尽可能地将javascript保留在Iphone之外有着既得利益......他们似乎想要通过他们的应用商店通过要求本地运行来管理事物..我很好奇,如果javascript也很慢Android手机,(我之前从未使用过一款)..如果它不是那么我觉得有点奇怪的是,Iphone会慢慢使用javascript,无论如何,它们已经失去了市场份额并将不得不解决问题在某些方面,我确信,我认为人们正在抓住苹果的游戏和白痴试图微观管理一切,因为移动设备领域出现了更多合法的替代品。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM