Comparison between Flutter and Android native WebView

Comparison between Flutter and Android native WebView

Preface

Since Google launched the flutter cross-platform development framework, flutter has been very popular in various technical forums.

When it comes to cross-platform development, WebView must be mentioned. WebView can be said to be the cheapest cross-platform development solution. We know that flutter can be mixed with native development, and they can call each other. So when we do mixed development, if we need to use WebView, should we call the native WebView or use flutter to implement WebView? If we use flutter to implement WebView, how does its performance compare to native? Today we will take Android as an example to actually test it from several different dimensions!

[[265159]]

Flutter implements WebView

Flutter does not officially have a WebView component, but the powerful flutter-community forum has developed the flutter_webview_plugin plug-in to facilitate the use of WebView in flutter, taking into account the needs of the majority of developers.

The integration is simple, in the pubspec.yaml file:

  1. dependencies:
  2. flutter:
  3. sdk: flutter
  4. flutter_webview_plugin: ^0.3.0+2

All the following comparisons are based on Android's native WebView and the flutter_webview_plugin plug-in. For the sake of rigor, no comparison is made with third-party WebView.

Test phone: Xiaomi 8SE System: Android 8.1.0

Loading speed comparison

To test the speed of opening a web page, you only need to get the timestamps when WebView starts loading the web page and when the web page is loaded. The difference between the timestamps is the time it takes to open the web page. We print logs at the corresponding locations in Android native and flutter respectively:

  1. webView?.webViewClient = object : WebViewClient() {
  2. override fun onPageStarted( view : WebView?, url: String?, favicon: Bitmap?) {
  3. Log.d(TAG, "onPageStarted:" + System.currentTimeMillis())
  4. super.onPageStarted( view , url, favicon)
  5. }
  6. override fun onPageFinished( view : WebView?, url: String?) {
  7. Log.d(TAG, "onPageFinished:" + System.currentTimeMillis())
  8. super.onPageFinished( view , url)
  9. }
  10. }
  11. Copy code
  12. flutterWebViewPlugin.onStateChanged.listen((state) {
  13. if (state.type == WebViewState.finishLoad) {
  14. print( 'finishLoad:' + DateTime.now().millisecondsSinceEpoch.toString());
  15. setState(() {
  16. isLoad = false ;
  17. });
  18. } else if (state.type == WebViewState.startLoad) {
  19. print( 'startLoad:' + DateTime.now().millisecondsSinceEpoch.toString());
  20. setState(() {
  21. isLoad = true ;
  22. });
  23. }
  24. });

To make the difference more obvious, we chose the more complex Sina homepage for loading comparison. To reduce the impact of the network on the loading speed, we connected the phone to the same network, performed 10 tests and then took the average value. In addition, we need to turn off the WebView cache to prevent the cache from affecting the loading speed:

  1. webView?.settings?.cacheMode = WebSettings.LOAD_NO_CACHE
  1. WebviewScaffold
  2. key : _scaffoldKey,
  3. url: widget.url,
  4. clearCache: true ,
  5. appCacheEnabled: false ,
  6. .
  7. .
  8. .
  9. );

The following is the data obtained by the author after 10 tests:

It can be found that the loading speed of flutter_webview_plugin is slightly faster than that of native WebView in the same environment, but the difference is not obvious and can be basically ignored.

Conclusion: flutter_webview_plugin loads slightly faster than native WebView.

Memory usage comparison

You can use the profiler tool that comes with AndroidStudio to test memory usage. We integrate the method of calling native WebView and flutter_webview_plugin in the flutter program to open the Taobao homepage and Sina homepage. When the program is just running, the memory usage is as follows:

Then use WebView to open the Taobao homepage:

Use flutter_webview_plugin to open the Taobao homepage:

It can be found that the memory usage of Taobao homepage opened with WebView is basically unchanged, but the memory usage of Taobao homepage opened with flutter_webview_plugin increases significantly, and the fluctuation is large.

Conclusion: flutter_webview_plugin occupies more memory than native WebView.

HTML5 compatibility comparison

You can score the browser compatibility in html5test. The test found that the scores of native WebView and flutter_webview_plugin are as follows:

Now on Xiaomi 8SE mobile phone, the HTML5 compatibility scores of native WebView and flutter_webview_plugin are both 501.

Conclusion: There is no significant difference in HTML5 compatibility between native WebView and flutter_webview_plugin.

Summarize

We compared native WebView and flutter_webview_plugin in terms of web page loading speed, memory usage, and HTML5 compatibility, and found that native WebView occupies less memory, and there is no significant difference in web page loading speed and HTML5 compatibility.

In actual use, since flutter_webview_plugin does not exist in the widget tree, notification interaction widgets such as snackbars, dialogs, etc. cannot be used in flutter_webview_plugin. However, flutter_webview_plugin has the advantage of being cross-platform. If you need to use WebView on both Android and iOS in a flutter project, it is recommended to use flutter_webview_plugin. Otherwise, it is recommended to use native WebView.

I hope everyone will actively discuss, exchange your valuable experiences, and improve each other!

<<:  Draw Icons from Scratch Series: Detailed Explanation of Icon Application

>>:  Apple Card physical card exposed, only logo but no card number

Recommend

Baidu promotion invalid clicks, how to avoid invalid clicks?

Friends who are engaged in Baidu bidding promotio...

Get APP competitive product analysis

The overall feeling that Get gives me is that it ...

For the first time, scientists mentioned RNA from extinct animals!

Abstract: It is of great significance to the resu...

Analyze Pinduoduo's event operation system and coupon gameplay!

Pinduoduo’s slogan is “More affordable, more fun”...

What to do if you accidentally lose your phone? It is crucial to do these things

Going out for a holiday, booking attractions, tak...

Five little-known facts about peony

(Copyrighted image from the gallery, no permissio...

JavaScript has a super bug, all X86/ARM processors are affected

On the 15th of this month, researchers from VUSec...

Aiti Tribe Stories (29): What is it like to develop transformation testing?

[51CTO.com original article] Gavin has 20 years o...

About the fact that I became ugly at work...

Review expert: Zhang Yuhong, chief physician of t...

Case analysis: How to build a user incentive system?

The construction of a user incentive system is ge...

Use private domain traffic to achieve ROI of 1:100 for educational products

I have been wanting to write this article for a y...