This is the unpublished documentation of wasm-bindgen, the published documentation is available on the main Rust and WebAssembly documentation site . Features documented here may not be available in released versions of wasm-bindgen.


View full source code or view the compiled example online

Want to profile some Rust code in the browser? No problem! You can use the API and friends to get timing information to see how long things take.


# #![allow(unused_variables)]
#fn main() {
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use wasm_bindgen::prelude::*;

// lifted from the `console_log` example
extern "C" {
    #[wasm_bindgen(js_namespace = console)]
    fn log(a: &str);

macro_rules! console_log {
    ($($t:tt)*) => (log(&format_args!($($t)*).to_string()))

pub fn run() {
    let window = web_sys::window().expect("should have a window in this context");
    let performance = window
        .expect("performance should be available");

    console_log!("the current time (in ms) is {}",;

    let start = perf_to_system(performance.timing().request_start());
    let end = perf_to_system(performance.timing().response_end());

    console_log!("request started at {}", humantime::format_rfc3339(start));
    console_log!("request ended at {}", humantime::format_rfc3339(end));

fn perf_to_system(amt: f64) -> SystemTime {
    let secs = (amt as u64) / 1_000;
    let nanos = ((amt as u32) % 1_000) * 1_000_000;
    UNIX_EPOCH + Duration::new(secs, nanos)