简体   繁体   English

phonegap / cordova应用程序比PhoneGap Desktop慢

[英]phonegap/cordova app is slower compare to PhoneGap Desktop

Recently I just started to build my app by using phonegap/cordova. 最近,我刚刚开始使用phonegap / cordova来构建我的应用程序。 At the beginning, I downloaded the Phonegap Desktop and carry out the testing on Phonegap Desktop during development phrase. 最初,我下载了Phonegap Desktop,并在开发阶段对Phonegap Desktop进行了测试。 It is powerful and flexible for testing purpose. 它功能强大且灵活,可用于测试。

When I am going to complete app, I build the app by using cordova CLI, then install the apk file on my actual device. 当我要完成应用程序时,我会使用cordova CLI来构建应用程序,然后在实际设备上安装apk文件。 I found that by using same coding and device , the app is significantly slower than Phonegap Desktop, especially on jquery animate and toggle modal active: 我发现通过使用相同的编码和设备 ,该应用程序的速度明显比Phonegap Desktop慢,尤其是在对jQuery animate和toggle modal active进行调试时:

// Simple code for toggle dialog modal to active
$("#modal-launcher, #modal-background, #modal-close").click(function () {
        $("#modal-content").css('top', Math.round( $(document).height() - ($("body").innerHeight() / 1.65 ))+'px');
        $("#modal-content,#modal-background").toggleClass("active");
    });



//Simple code for animate list item 
$( '.Class' ).animate({ right:'0px' }, 250);

The speed is same when I just open the app. 刚打开应用程序时的速度相同。 After I perform some actions to trigger events, I observed the performance is slowing down. 在执行一些触发事件的操作后,我发现性能正在下降。 For instance I need to wait for few seconds to let dialog prompt out, and the animate become slower as usual. 例如,我需要等待几秒钟以使对话框提示,并且动画照常变慢。 If i restart the app, the performance is become normal again until I repeat same process. 如果我重新启动应用程序,性能将再次恢复正常,直到我重复相同的过程。

The performance degradation is not just few milliseconds, it is a very significant difference that up to 4 - 6 seconds delay. 性能下降不仅是几毫秒,而且延迟高达4-6秒是一个非常显着的差异。 This would not happen if I switch to Phonegap Desktop, by using same coding and device . 如果我使用相同的编码和设备切换到Phonegap Desktop,则不会发生这种情况。 This make me have no idea about what is going on, as I thought they share the same framework. 这使我不知道发生了什么,因为我认为它们共享相同的框架。

I also tried build the app by using phonegap build android and ionic build android , but I get same result as well. 我也尝试通过使用phonegap build androidionic build android来构建应用程序,但是我也得到了相同的结果。 Does anyone has similar experience and has any solution for this issue ? 有没有人有类似的经验并且对此问题有任何解决方案?

The speed of the device and the version of webview is the important point. 设备的速度和Webview的版本很重要。 On the desktop you have a lot of power and it is okay to make the first steps there, but then switch to slow devices for testing and test the app also with older android versions. 在台式机上,您拥有强大的功能,可以在那里迈出第一步,但随后切换到速​​度较慢的设备进行测试,并使用较早的android版本测试该应用。 The reason is not the compatibility, the reason is, that older androids are slower. 原因不是兼容性,原因是较旧的android较慢。

If the app is running well on slower devices, it will work great on new devices. 如果应用在较慢的设备上运行良好,那么它将在新设备上正常运行。

Cordova itself and javascript are very fast. Cordova本身和javascript都非常快。 There are some mistakes which people make, when they start with cordova development and using jQuery/jQuerymobile: 当人们开始使用cordova开发并使用jQuery / jQuerymobile时,会犯一些错误:

  1. Write clean HTML with a clean structure, try to make the dom simple. 用干净的结构编写干净的HTML,并尝试简化dom。

  2. Don't query the dom with jquery over and over again. 不要一遍又一遍地用jquery查询dom。 Instead put your selector in a variable, if you use it several times: 如果多次使用选择器,则将其放在变量中:

 var $selector = $("#myselector); if ($selector.height() > 100){ // do something } 

  1. Concatenate jQuery operations: 串联jQuery操作:

  $("#myselector).css("active").text("my new text"); 

  1. Use find(), next(), parent(), …. 使用find(),next(),parent(),…。 If you need to have multiple operations on multiple elements, and the elements are not fare away in the dom, then use find(), next(), parent(), … it is much faster then querying the whole dom. 如果您需要对多个元素进行多项操作,而这些元素在dom中并不遥不可及,则可以使用find(),next(),parent(),…比查询整个dom快得多。

  2. Don't use jQuery for everything. 不要将jQuery用于所有内容。 jQuery is great, but a lot of operations can be done with simple javascript. jQuery很棒,但是可以使用简单的javascript进行很多操作。

  3. Don't use jQuery-plugins for things that can be done easily with javascript. 不要将jQuery插件用于使用javascript可以轻松完成的事情。

  4. Make a single-page-html-app and avoid to use multiple html-pages. 制作一个单页面html应用,并避免使用多个html页面。 This is the best way to slow down the speed of your app and to make the development complicate. 这是减慢应用程序速度并使开发复杂化的最佳方法。

  5. Use a custom jQuery download and don't include things you don't need. 使用自定义jQuery下载,不要包含不需要的内容。

  6. Use tap instead of click. 使用点击而不是点击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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