'Converting 2d js arrays to go (golang) slice of struct in webassembly takes too much time

Good time of day to all of you! I have an 2d array which consists of a couple of thousand points like [[x, y, some_data],[x, y, some_data],[x, y, some_data]...[x, y, some_data]] I have an algorithm which is processing all of those points. I want to speed it up using golang, because this algorithm can be run hundred of times by user with different sets of points. In order to process those values in go I'm sending this 2d array to go, receive it ad an []interface{} and then converting it to slice of Points (struct):

type Point struct {
    x         float64
    y         float64
    some_data float64
}
func I2PointArray(src js.Value) []Point {
    srcLen := src.Length()
    dst := make([]Point, srcLen)
    for i := 0; i < srcLen; i++ {
        x := src.Index(i).Index(0).Float()
        y := src.Index(i).Index(1).Float()
        some_data := 0.0
        if !src.Index(i).Index(2).IsUndefined() {
            some_data = src.Index(i).Index(2).Float()
        }
        dst[i] = Point{x, y, some_data}
    }
    return dst
}

And as for the algorithm speed I got boost up to 8x , where js implementation took 16ms to proceed, golang proceeded it for 2ms

BUT

if js took like 9ms Golang took around 270ms where 268ms are wasted on converting interface to Points slice. So is there any way I can speed this conversion process?

Thank you in advance!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source