Add Go and Rust algorithm benchmarks
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import "syscall/js"
|
||||
|
||||
//go:wasmexport recursive
|
||||
func recursive(n uint64) uint64 {
|
||||
if n < 2 {
|
||||
return n
|
||||
}
|
||||
return recursive(n-1) + recursive(n-2)
|
||||
}
|
||||
|
||||
//go:wasmexport iterative
|
||||
func iterative(n uint64) uint64 {
|
||||
var a, b uint64 = 0, 1
|
||||
for i := uint64(0); i < n; i++ {
|
||||
a, b = b, a+b
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
//go:wasmexport identity
|
||||
func identity(n uint64) uint64 {
|
||||
return n
|
||||
}
|
||||
|
||||
func register(name string, fn func(uint64) uint64) {
|
||||
js.Global().Set(name, js.FuncOf(func(_ js.Value, args []js.Value) any {
|
||||
return float64(fn(uint64(args[0].Int())))
|
||||
}))
|
||||
}
|
||||
|
||||
func main() {
|
||||
register("goRecursive", recursive)
|
||||
register("goIterative", iterative)
|
||||
register("goIdentity", identity)
|
||||
select {}
|
||||
}
|
||||
Reference in New Issue
Block a user