本文介绍了用于繁重计算的 WebAssembly + WebView + Javascript 接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在开发这个应用程序,它目前是为 web、android 和 ios 本地编写的.这个应用程序在后台做了一些繁重的工作,我们正在寻找改进的方法,但保持三个基本相同代码的来源很糟糕.

So I'm working on this app which currently are written natively for the web, android and ios. This app does some heavy lifting in the background which we are looking for ways to improve, but keeping three sources of basically the same code sucks.

一种方法是用 C++ 制作一个库,然后在应用程序中使用,并通过 WebAssembly 在网络上使用.然而,这需要我们生成新版本的 android/apps 并将它们发布到各自的商店.

One way is to make a library in C++ which is then used in the apps and through WebAssembly on the web. This however requires us to generate new versions of the android/apps and publish them to their respective stores.

但后来我认为 WebAssembly 可能适用于所有平台.但是由于我们仍然希望在移动平台上提供原生体验,我认为可以使用 WebView 与 javascript 界面一起与 WebAssembly 中的库进行通信并以原生方式呈现数据.对库的更新只需执行一次,并且所有应用始终具有库的最新版本.

But then I thought that WebAssembly might be usable on all platforms. But since we still want to offer a native experience on the mobile platforms I'm thinking that a WebView could be used to with a javascript interface to communicate with the library in WebAssembly and present the data natively. Updates to the library would only have to be done once and all apps always has the latest version of the library.

这是可能的吗,它还能提供高性能吗?webview 和 javascript 接口会成为瓶颈吗?

Would this be possible and would it still offer high performance? Would the webview and javascript interface be bottlenecks?

推荐答案

据我所知,你有一个原生 Web 应用(HTML + JavaScript)和原生 Android(Java 或 Kotlin)和 iOS(Swift 或 Objective-C)应用.该应用程序做了一些具有性能问题的繁重工作 - 我猜这些问题只出现在应用程序的网络(即 JavaScript)版本中?您想提高此代码的性能,并在所有三个平台上共享它吗?

From what I understand you have a native Web App (HTML + JavaScript) and native Android (Java or Kotlin) and iOS (Swift or Objective-C) apps. The app does some heavy lifting that has performance issues - I am guessing that these issues only arise in the web (i.e. JavaScript) version of the app? You want to improve the performance of this code, and also share it across all three platforms?

如果是这样,我认为 WebAssembly 值得考虑.您可以使用 C++ 创建一个在 iOS 和 Android 中原生运行的库,并通过 EmScripten 编译为 WebAssembly 以在 Web 上运行.

If that is the case, I think WebAssembly is worth considering. You can create a library in C++ which is run natively in iOS and Android, and compiled to WebAssembly via EmScripten to run on the web.

需要记住的一点是,WebAssembly 没有内置 IO.因此,与您的代码的所有交互都必须跨越 JavaScript/WebAssembly 边界,这确实会导致惩罚.因此,这种方法最适用于具有简单接口的算法,例如他们可能会对少量数据进行大量计算,然后返回结果.

One things to bear in mind is that WebAssembly has no built-in IO. Therefore all interactions with your code would have to cross the JavaScript / WebAssembly boundary, which does incur a penalty. Therefore, this approach works best for algorithms that have a simple interface, e.g. they might perform a lot of calculations on a small amount of data, then return the result.

我不知道您为什么要考虑在 WebView 中运行它?对于 iOS 和 Android,您不需要使用 WebAssembly - 您可以在本地运行 C++ 代码.

I am not sure why you are considering running this in a WebView? For iOS and Android you will not need to use WebAssembly - you can run the C++ code natively.

这篇关于用于繁重计算的 WebAssembly + WebView + Javascript 接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 07:37