Comparison of the efficiency of various JsBridge calls

In the previous project, it was necessary to realize the function of clicking a keyword on the H5 page of the WebView to jump to the specified page of the Native terminal. Google used the ShouldOverrideUrlLoading method to do this. Later, to optimize the function of this part, we specially used a period of time to do some tests and comparisons. Now put the data and conclusions up for your reference.

At present, there are about six commonly used communication methods between Web pages and Native terminals. The following performance tests will be conducted for these six methods to select the optimal solution.

Participating model and system version:

  1. Moto Nexus6 OS: the 6.0.1
  2. Meizu 3 s OS: 5.1.1
  3. Red rice 1 OS: 4.2.2

Test cases:

The current time T is recorded when the request is initiated on the Web page, and the time T is transmitted to the Native terminal through JsBridge pseudo-protocol. The current system time T2 is recorded when the Native terminal receives the request. T2-t is the communication time between the Web terminal and Native terminal. Perform the same operation 20 times, comparing performance by average time.

Test functions

1.ShouldOverrideUrlLoading

Page request code:



The Native end intercepts the main resource request by overloaded ShouldOverrideUrlLoading method and parses the JsBridge pseudo-protocol to obtain relevant data.

2.ShouldInterceptRequest

Page request code:



The Native end intercepts the sub-resource request by overloaded ShouldInterceptRequest method and parses the JsBridge pseudo-protocol to obtain relevant data.

Android 5.0 provides the following interception methods:

Android 5.0 and later provide interception methods to obtain richer page information. The WebResourceRequest methods are: getUrl(), isForMainFrame(),hasGesture(),getMethod(),getRequestHeaders(); The getRequestHeaders() method gets the most requested information.



3. Prompt way

Page request code:



The onJsPrompt method of Native terminal rewrites WebChromeClient to handle pseudo protocol requests:

4. The Alert

Page request code:



The onJsAlert method of Native end rewrites WebChromeClient to handle pseudo protocol requests:

5. Confirm

Page request code:



Native end redo WebChromeClient onJsConfirm method pull processing bogus protocol request:

6. The Console mode

Page request code:



Native end redo WebChromeClient onConsoleMessage method pull processing pseudo protocol request:

The test results

The data is shown below:



The test results

According to the above data, Confirm is generally the shortest and most stable mode. If you don’t need more detailed Information on the Web side, use Confirm for best performance. However, if you need to read the Web headers and whether the request was initiated by the main frame, you must use a sub-resource intercept method. This rich request information is necessary for the extension of permission control.

About the addJavascriptInterface

This article was never intended to add the addJavascriptInterface method to the comparison data in the first place, as it had serious security vulnerabilities prior to Android4.2. Google solved this problem with the @javascriptinterface annotation in versions after Android4.2. This way, as Google’s official Webview and JavaScript solution, will certainly be better than other interactive ways. The wheels of history roll forward, most of the current users of the system version are focused on 4.4.2 above, so today or this way to do a test. The test data also confirmed my guess that the time interval was the shortest. So, if your App only plans to support Android4.2 or higher, consider this official solution.



Fixation of trailer

The next article will cover complete JS interactions based on Confirm and Intercept, which include Native methods that call the Web side. Android WebView: How do I Flirt with JavaScript?