Let's see how good can WASM really be.

This is a small project built with the intention of testing Web Assembly. It's main purpose is to compare the execution times between vanilla Javascript and Web Assembly.

# Order an array strings

Lets start with an array with 20 000 random names, and see how fast each approach handles this. One small thing we needed to do, was to deconstruct the array ([...randomNames]) to stop mutation. To be fair, this could be done before-hand, but it shouldn't make much difference at all (I tested)

`[...randomNames].sort();`

`sort.Strings(randomNames)`

# Remove repeated string in array

Considering both of the sorting algorithms make in-place changes (mutating the original array) let's now try and see which of them can remove the many repeated names off of this array.

```
return array.reduce((acc, item) => {
if (acc.find((i) => i === item)) {
return acc;
}
acc.push(item);
return acc;
}, []);
```

```
keys := make(map[string]bool)
list := []string{}
for _, entry := range intSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
```

# Fast Fourier Transform (Cooley-Tukey)

Now using a random signal of length 8192, we're going to compare how fast each aproach can compute an FFT. This one should definitely be a bigger challenge and maybe now we can see where WASM would shine.

```
FFT(re, im) {
var N = re.length;
for (var i = 0; i < N; i++) {
for(var j = 0, h = i, k = N; k >>= 1; h >>= 1)
j = (j << 1) | (h & 1);
if (j > i) {
re[j] = [re[i], re[i] = re[j]][0]
im[j] = [im[i], im[i] = im[j]][0]
}
}
for(var hN = 1; hN * 2 <= N; hN *= 2)
for (var i = 0; i < N; i += hN * 2)
for (var j = i; j < i + hN; j++) {
var cos = Math.cos(Math.PI * (j - i) / hN),
sin = Math.sin(Math.PI * (j - i) / hN)
var tre = re[j+hN] * cos + im[j+hN] * sin,
tim = -re[j+hN] * sin + im[j+hN] * cos;
re[j + hN] = re[j] - tre; im[j + hN] = im[j] - tim;
re[j] += tre; im[j] += tim;
}
}
```

```
func FFT(a []complex128, n int) []complex128 {
x := make([]complex128, n)
copy(x, a)
j := 0
for i := 0; i < n; i++ {
if i < j {
x[i], x[j] = x[j], x[i]
}
m := n / 2
for {
if j < m {
break
}
j = j - m
m = m / 2
if m < 2 {
break
}
}
j = j + m
}
kmax := 1
for {
if kmax >= n {
return x
}
istep := kmax * 2
for k := 0; k < kmax; k++ {
theta := complex(0.0, -1.0*math.Pi*float64(k)/float64(kmax))
for i := k; i < n; i += istep {
j := i + kmax
temp := x[j] * cmplx.Exp(theta)
x[j] = x[i] - temp
x[i] = x[i] + temp
}
}
kmax = istep
}
}
```

Disclaimer

The algorithms used in this demo are far from optimized, and are just some random code I made up on the spot, or got from the internet.

This was intended to be more of a test to WASM's capabilities than a reliable benchmark.

Feel free to suggest any alteration or point out any mistake I might have done!