Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

performance.now in worker #1752

Closed
dakom opened this issue Sep 5, 2019 · 7 comments · Fixed by #3506
Closed

performance.now in worker #1752

dakom opened this issue Sep 5, 2019 · 7 comments · Fixed by #3506
Labels

Comments

@dakom
Copy link
Contributor

dakom commented Sep 5, 2019

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 dakom added the bug label Sep 5, 2019
@fitzgen
Copy link
Member

fitzgen commented Sep 5, 2019

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?

@jrandall
Copy link

jrandall commented Apr 4, 2020

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.

@knpwrs
Copy link

knpwrs commented Sep 21, 2020

This issue causes the wasm-timer crate to break for web workers: tomaka/wasm-timer#12

@tiby312
Copy link

tiby312 commented Dec 27, 2021

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

@UsernameN0tAvailable
Copy link

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!

delapuente added a commit to delapuente/qasmsim that referenced this issue Mar 21, 2023
There is no way to access the `performance` object from `web_sys`
inside web workers because `web_sys` only exposes the `Window` object, not
available in a worker context. Instead, as workaround, we use `wasm_bindgen`
with `extern "C"` to access the `performance` object directly.

rustwasm/wasm-bindgen#1752
https://rustwasm.github.io/wasm-bindgen/reference/attributes/on-js-imports/js_name.html
@allsey87
Copy link
Contributor

allsey87 commented Jul 5, 2023

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

@daxpedda
Copy link
Collaborator

daxpedda commented Jul 5, 2023

Fixed by #3506.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants