Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to choose JavaScript engine for application development on iOS and Android

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to choose JavaScript engine for application development on iOS and Android". In daily operation, I believe many people have doubts about how to choose JavaScript engine on iOS and Android for application development. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to choose JavaScript engine for application development on iOS and Android". Next, please follow the editor to study!

How to use JavaScript on IOS/Android

There are two main ways. One is to use the browser components of the system (UIWebView in IOS and WebView in Android), and the other is to use an integrated JavaScript engine.

Using the browser components of the system is easier to implement but more complex and inefficient. WebView provides addJavascriptInterface with Java

The method of injecting classes into JavaScript text. But it only supports the most primitive data types, so it also limits API design. And it is unstable on the Android 2.3simulator, and you will encounter issue on the real machine.

The problem of # 12987. Worse on IOS, UIWebView doesn't have a public APIs that supports JavaScript-to-Objective-C interaction (you have to use a similar APIs to achieve the same functionality as addJavascriptInterface).

PhoneGap is a well-known project based on UIWebView and WebView. Developers are forced to use callback functions from JavaScript

APIs gets the return value. This is extremely inefficient and more complex in the game.

Early ngCore also relied on UIWebView to support iOS. But this mechanism has been replaced by its poor performance.

In order to achieve better performance, flexibility and compatibility, embedded full-featured JavaScript engine becomes more effective.

Select JavaScript engine

As far as I know, there are four JavaScript engines that can run on iOS or android: JavaScriptCore, SpiderMonkey, V8 and Rhino. The following table shows the various engines in iOS

Compatibility with Android

IOSAndroidJavaScriptCoreInterpreter onlyInterpreter and JITSpiderMonkeyInterpreter onlyInterpreter and JITV8JIT only for jailbroken devicesJITRhinoUnsupportedInterpreter

When I designed OpenAphid-Engine to be a suitable Javascript engine, I mainly considered the following indicators:

Compatibility: both iOS and Android simulators and devices on x86 and ARM platforms are supported.

Stability. Run stably on the corresponding platform and CPU architecture.

Expansibility. It can be easily extended by using local features. For example, OpenAphid-Engine implements OpenGL through Javascript through a bridge layer

The use of ES.

Good performance: a fast Javascript engine boils down to two factors: an effective binding mechanism and low overhead. . OpenAphid-Engine triggers hundreds of OpenGL through JavaScript when rendering a single page

ES is called for rendering. This makes a lot of sense, and if you just put the overhead on simply executing the JavaScript, it will result in slow rendering.

Small.: it should be relatively small in memory footprint and on its own execution files.

Rhino and V8 appeared first, but iOS is not supported. I would very much like to use V8 to develop OpenAphid-Engine. When I first used it, I found that it had an elegant code structure and good performance, but I was very disappointed because V8 can only be used in JIT mode, but IOS does not support it. Unless you use a jailbroken device. (please refer to issue for details

# 1312)

I've been struggling with JavaScriptCore and SpiderMonkey for a long time. After successfully deploying the Android and IOS projects, I found a better one through experiments.

SpiderMonkey is easy to get development permissions, but is at a disadvantage when compared to JavaScriptCore. SpiderMonkey produces a large number of binaries

(about 1.3MB on ARMv7); JavaScript executes more slowly, and the bridge performance between JavaScript and C++ is more important. Another thing that kept me away from SpiderMonkey was the random crash on the iOS simulator.

JavaScript engines are affected by many things, such as the version of the cross-compiler, the version of the engine, and the type of operating system. The following table lists the running times of several engines running on iPod Touch 4. (if you are interested, please contact Google

Doc to see the exact time)

JavaScriptCore took the lead by a wide margin.

I couldn't find SpiderMonkey, so I used the following three custom builders Cocos2d-iPhone-2.1-beta4, Cocos2d-x-2.1-beta3, and iMonkey.

All tested apps are based on LLVM version 4.1, and all engines are running in interpreter mode (iOS restricted).

Introduction of several benchmarks:

1m-js_loop performs an empty loop a million times.

1m-native_function requests calling a local function that returns undefined a million times

1m-js_function is the same as the previous one, except that it is replaced by JavaScript.

Fib (30) calculates Fibonacci (30) recursively.

Sudoku-5 uses the algorithm here to solve the Sudoku problem.

1m-native_function JavaScriptCore uses portable C

The APIs implementation, of course, is not the most efficient way to introduce native functions.

SpiderMonkey runs faster on desktops because of advanced JIT tracking methods, but the opposite is true on IOS devices.

On most benchmarks, using iMonkey is faster than SpiderMonkey

Obviously, using SpiderMonkey will get better performance on iOS. NgCore 1.10 adds custom features to iOS, so it is better than variants like SpiderMonkey.

Challenges to JavaScript Code

After I focused on JavaScriptCore, my research went one step further:

1. It runs a million times native_function and a million times Math.abs (0) six times as long as it uses JavaScriptCore. I have observed that the same performance problem occurs when accessing the properties of an object through injection.

two。 Make use of C

Although the development of APIs is simple, it lacks flexible memory management mechanism. Without an advanced internal garbage collection mechanism, it is difficult to solve problems similar to circular.

The problem with references.

3. Many official versions of JavaScriptCore are available

. But OpenAphid-Engine is a better one. It is not only fast, but also quite small.

I abandoned the original use of C.

The APIs scheme thus solves problems 1 and 2. The version of JSC used comes from iOS4.3.3 because it is also in parser mode with faster and smaller execution files than the version from iOS 5.

JS engine used on other products

During the development of OpenAphid-Engine, I have been keeping an eye on other engines. The following table summarizes the usage of other JS engines.

IOSAndroidngCore 1.6 and aboveUIWebViewV8ngCore 1.7 and laterSpiderMonkeyV8TitaniumJavaScriptCoreV8 or RhinoPhoneGapUIWebViewWebViewCocos2D-x JavaScriptSpiderMonkeySpiderMonkeyCocoonJSJavaScriptCoreJavaScriptCoreEjectaJavaScriptCoreUnsupporteddirectCanvasJavaScriptCoreNo clue, this is the end of the study on "how to choose JavaScript engine for application development on iOS and Android". I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report