Hi, is there a way to instantiate JsArray? Similar...
# webassembly
a
Hi, is there a way to instantiate JsArray? Similarly to Int.toJsNumber. I am trying to create a JsArrayJsNumber to check execution of jsExported method locally Is there a method allowing to multiply JsNumbers directly without having to cast to Int? I am checking how does code perform on matrix multiplication and I didnt want to create overhead with casting
As for now wasm version performs significantly worse than native kotlin version for 100x100 matrix (69 ms vs 4 ms). I started out from nodejs example. Are there any optimization flags by any chance or something I should remember about? Or should I assume that it's still early stage and shortcomings are expected?
Copy code
@JsExport
fun execute(matrix1: JsArray<JsNumber>, matrix2: JsArray<JsNumber>, size: Int) {
    val result = MutableList(size * size) { 0.0 }
    for (i in 0 until size) {
        for (j in 0 until size) {
            var sum = 0.0
            for (k in 0 until size) {
                val num1 = matrix1[i * size + k]!!.toInt()
                val num2 = matrix2[k * size + j]!!.toInt()
                sum += num1 * num2
            }
            result[i * size + j] = sum
        }
    }
}
a
@Svyatoslav Kuzmich [JB] ^^
s
It is expected that
JsArray
is slow, it performs non-inlined Wasm->JS call for each element access. To speed things up, it might be better to copy
JsArray
into
IntArray
or
DoubleArray
just once, and then do a lot of repeated actions on them:
Copy code
val fasterMatrix1 = IntArray(size) { matrix1[it]!!.toInt() }
🙌 1
a
Thanks! Now it has close to native performance 💯
👍 1