wasm-bindgen icon indicating copy to clipboard operation
wasm-bindgen copied to clipboard

performance.now in worker

Open dakom opened this issue 4 years ago • 5 comments

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?

dakom avatar Sep 05 '19 15:09 dakom

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?

fitzgen avatar Sep 05 '19 16:09 fitzgen

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.

jrandall avatar Apr 04 '20 01:04 jrandall

This issue causes the wasm-timer crate to break for web workers: https://github.com/tomaka/wasm-timer/issues/12

knpwrs avatar Sep 21 '20 13:09 knpwrs

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;
}

tiby312 avatar Dec 27 '21 19:12 tiby312

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!

UsernameN0tAvailable avatar Jun 16 '22 03:06 UsernameN0tAvailable

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();

allsey87 avatar Jul 05 '23 11:07 allsey87

Fixed by #3506.

daxpedda avatar Jul 05 '23 13:07 daxpedda