Skip to content

Help needed: Fixing performance problems w/js_sys::Math::random #3196

Answered by Liamolucko
NateLevin1 asked this question in Q&A
Discussion options

You must be logged in to vote

You could give the rand crate a go, which includes random number generators written in Rust that could potentially be faster than using Math.random. To seed them from the browser's RNG, you'll need to enable the js feature of the getrandom crate, like so:

# Cargo.toml
[dependencies]
rand = "0.8.5"
getrandom = { version = "0.2.8", features = ["js"] }

Math.random wouldn't be slow because of the JS glue code, though, because it doesn't have any - all it does is return a single f64, which wasm can accept directly without any glue.

I haven't actually checked whether rand is faster. If you don't need a cryptographic RNG, rand::rngs::SmallRng should also be faster than its default RNG.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@NateLevin1
Comment options

@Liamolucko
Comment options

Answer selected by NateLevin1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants