Skip to main content

rust_css_parser/utils/
time.rs

1//! `performance.now()`: milliseconds. native: a monotonic clock, wasm: the host clock (imported `css_now`)
2
3#[cfg(not(target_arch = "wasm32"))]
4pub fn now() -> f64 {
5    use std::sync::OnceLock;
6    use std::time::Instant;
7    static START: OnceLock<Instant> = OnceLock::new();
8    START.get_or_init(Instant::now).elapsed().as_secs_f64() * 1000.0
9}
10
11#[cfg(target_arch = "wasm32")]
12pub fn now() -> f64 {
13    #[link(wasm_import_module = "env")]
14    extern "C" {
15        fn css_now() -> f64;
16    }
17    // SAFETY: the host provides `css_now`
18    unsafe { css_now() }
19}
20
21/// `${ms.toFixed(2)}ms`
22pub fn format_ms(ms: f64) -> String {
23    format!("{}ms", crate::utils::number::to_fixed(ms, 2))
24}