wasm-bindgen
wasm-bindgen copied to clipboard
performance.now in worker
MDN says that WorkerGlobalScope has performance
in Chrome and Firefox: https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/performance
web-sys
doesn't seem to export this: https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.WorkerGlobalScope.html
Is this expected? If so, what is the right way to get performance.now
from inside a worker?
It looks like our webidl, which is derived from Firefox/Gecko, only has it on the Window
interface, not on WorkerGlobalScope
. I wonder if this is supported in workers cross browser?
It looks like performance on worker global scope is part of the High Resolution Time Level 2 recommendation, and it is defined as an attribute on the WindowOrWorkerGlobalScope
mixin rather than directly on WorkerGlobalScope
itself: https://www.w3.org/TR/hr-time-2/#the-performance-attribute
The link from that spec to caniuse.com seems to indicate most browsers have full support for it: https://caniuse.com/#feat=high-resolution-time
I spent a few hours looking into this today, as I was trying to use the backoff
crate that claims to work with wasm-bindgen (using the instant
and rand
crates with the wasm-bindgen
feature enables), but it turns out that only works on the main thread as it uses web_sys::window()
to access performance
and I was trying to use it on a web worker.
This issue causes the wasm-timer
crate to break for web workers: https://github.com/tomaka/wasm-timer/issues/12
As a workaround, I added this and it let me call performance.now() inside of webworker:
#[wasm_bindgen]
extern "C" {
#[no_mangle]
#[used]
static performance:web_sys::Performance;
}
From which context is perfomance called from on Firefox/Geeko? I suppose it's still WorkerGlobalScope right? so it's should be an easy fix!
Another workaround until this is fixed could be:
let performance = js_sys::Reflect::get(&js_sys::global(), &"performance".into())
.expect("failed to get performance from global object")
.unchecked_into::<web_sys::Performance>();
let time = performance.time_origin() + performance.now();
Fixed by #3506.