Showing posts with label jQuery Mobile. Show all posts
Showing posts with label jQuery Mobile. Show all posts

Wednesday, August 14, 2013

How jQuery Mobile Eats PhoneGap Performance, See Experiment


As we know, jQuery Mobile renders a lot of HTML for a simple element/widget. For example, the below picture(from jQM demos) shows the HTML source code (right) of a simple list item(left).
However, to make such a list item, the html can be as simple as just: <li>Acura</li>(don’t know how? Leave a comment.)
But how will HTML elements affect the performance? Let’s take an experiment to see.
I wrote a simple page to test it. Open live demo for web browser(PC & Phone)
 <!DOCTYPE html>  
 <html xmlns="http://www.w3.org/1999/xhtml">  
   <head>  
     <meta charset="utf-8" />   
     <meta name="viewport" content="initial-scale=1, width=device-width" />  
     <title></title>  
     <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js"></script>  
   </head>  
 <body>  
   <p>  
     <input type="button" value="Render jQM" id="btnRenderjQM"/>  
     <input type="button" value="Render Custom" id="btnRenderCustom"/>  
   </p>  
   <p><b>Time: </b><span id="time">0</span></p>  
   <ul id="result" style="height: 200px; overflow: auto;"></ul>  
   <script language="javascript" type="text/javascript">  
     var time = null;  
     $(document).ready(function() {  
       var li1 = '<li data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="c" class="ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-first-child ui-btn-up-c">\  
               <div class="ui-btn-inner ui-li">\  
                 <div class="ui-btn-text"><a href="#" class="ui-link-inherit">Acura</a></div>\  
                 <span class="ui-icon ui-icon-arrow-r ui-icon-shadow">&nbsp;</span></div>\  
             </li>';  
       var li2 = '<li>Acura</li>';  
       $("#btnRenderjQM").click(function() {  
         var $ul = $("#result");  
         $ul.empty();  
         var html = '';  
         for (var i = 0; i < 1000; i++) {  
           html += li1;  
         }
         time = new Date();    
         $ul.html(html);  
         $("#time").html(new Date() - time);  
       });  
       $("#btnRenderCustom").click(function() {  
         var $ul = $("#result");  
         $ul.empty();  
         var html = '';  
         for (var i = 0; i < 1000; i++) {  
           html += li2;  
         }
         time = new Date();    
         $ul.html(html);  
         $("#time").html(new Date() - time);  
       });  
     });  
   </script>  
 </body>  
 </html>  
There are two buttons on the test page, one appends 1000 jQueryMobile list items to the list, while the other appends 1000 simple list items.
Let’s see some charts.
Test in firefox:

Test in google chome:

Test on Android Phone:
The charts mean: HTML elements eat performance. If we use simple HTML elements to render a page, it will save about 80% performance than using jQM. If it’s a small page, that would be OK using jQM, if it’s a complex page, I recommend you write the HTML elements by yourself.
The above test page can be viewed online here: live demo for web browser(PC &Phone).
Feel free to make comments or ask questions.

Monday, August 12, 2013

My PhoneGap With jQuery Mobile Development Experience

Recently my PhoneGap team has been working with jQuery Mobile for a few clients. The overall development experience is really bad.

Project 1: tablet

The first APP I want to say is for tablet. Our team joined the development when the APP was about 90% done. That APP used jQuery Mobile and also backbone.
When we joined the development, the code looked not very professional. There are mainly two files for the project, two very big ones. One is the index.html that had over 2000 lines, and the other is index.js that has over 5000 lines of code.
Our first tasks are very small and were done quickly, but we still suffered from the too large code. So we advised the client refactor, and the client accepted. We then re-designed the code architecture and separated the big index.js into many small ones based on functions. The JS code became much clear, but the index.html is still very large(no change).
Later the client said the whole APP performance was not good, then we tried many methods but finally we have to remove usage of jQuery Mobile. It is because jQuery Mobile generates too many html elements for a simple element, such as a button. When the data become a little large, it costs a lot of time rendering so many html element. And the html elements each performance during the whole lifetime of the page.
Another example:
For the above item template, we can change it to:
<li>Acura<span></span></li> , even the <span> is not necessary.
For some pages with big data, we refactored the html to use as few html as possible. And the performance become much better.

Project 2: iPhone

We joined the project also from half way. One of our tasks is to list all contacts and search and choose. Normally, a phone has more than 200 contacts. At the beginning, we used jQuery Mobile listview because the project was already using jQuery Mobile. The list item template is not complex, but the performance was very bad. It cost about 3 seconds to render 300 contacts.
Then we tried removing using listview, instead, we wrote a very simple UL, and the performance become much better. We also spent much time on the listview item template, since it doesn’t work well and not easy to use.
For this project, there are also mainly two big files: index.js and index.html. The code is hard to read or maintain. We need to press CTRL+F to navigate to the right page all the time. It’s really a nightmare.

In conclusion

The two projects originally used a lot of jQuery Mobile features, but finally many of them are removed due to performance issues. I think jQuery Mobile is only suitable for web applications, not for PhoneGap, since performance is really a big concern in PhoneGap.
Till this moment, I haven’t seen any good jQuery Mobile project architecture. A good code architecture should keep the code easy to read, easy to maintain, and any developer knows where to put the code. Generally, each file should have less than 300 lines of code, each method should have less than 30 lines of code.

Sunday, November 25, 2012

Who Is Murdering PhoneGap? It's jQuery Mobile

So many people are now arguing the bad performance of PG apps, because of that, many developers stopped entering the army of PG development.
Is PG really so bad?
The answer from me is absolutely NO.
I have to admit that there are so many bad PG apps. Why? Who made them? The most PhoneGap developers. But the developers are innocent of murdering PhoneGap. The real killer is jQuery Mobile.
The bad choice of jQuery Mobile for PhoneGap apps:
1. Not PhoneGap targeted.
jQuery Mobile is not targeting only for PhoneGap apps, instead, it’s developed for mobile WEBSITES. The pages/scripts/resources are organized in a website way, not a native app way. Take pages navigation for an example, a web app navigates to another page by changing the URL, but for desktop/native apps, we new an instance and put it into a wrapper control, the wrapper has full control to the new page, and can receive events of the page, which is quite different from web apps. So, if you are using jQuery Mobile to develop PhoneGap apps, you are probably missing a good application architecture.
2. Too fat & bad performance.
jQuery Mobile does provide very-very many controls/functions, but only a very small part of them are used in apps, which causes much waste of performance. I’d like to take a simple function for example, the scroll. As we know, most mobile browser cores don’t support fixed position, so we have to write custom code to do it and handle the scrolling. jQuery Mobile writes a lot of code for this function, at the same time, a few related touch functions are also provided, but in most cases, the developers only need the scroll. In a word, jQuery Mobile handles too much things for a simple action.
3. Bad documentation.
The jQuery Mobile documentation is not really documentation, but examples. Yeah, they are good examples, but absolutely bad documentation. Not all features are included in the examples, they missed many. This is quite hard for the developers. As some features are hidden from the developers, many developers have to write ugly code.
4. Critical bugs/issues not fixed.
There are so many posts about jQuery mobile scrolling and page flashing. Unfortunately, I don’t see a remarkable answer to their questions. I asked a few developers, they seem to have given it up, and leave the bug/issue as is.
How to make a good PhoneGap app? Here I can share some general ideas:
  • 1. Write your own javascript PhoneGap architecture if you are good at JavaScript. If you are not, you can follow me on codeplex where you can find PhoneGap architecture and demos.
  • 2. Don not write future code. You don’t need to write code that “may be used in future”, instead, write code that is really used.
  • 3. Resolve the critical bugs/issues by yourself. To make a good PhoneGap app, you must first be a JavaScript master, which means you can read others’ code and find the best solution for best of your app.
If you want to develop the best PhoneGap app, you can:
If you are looking for PhoneGap programmers or PhoneGap developers, please feel free to contact us. Free quote is available.