You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WebAssembly中获取纳秒级系统时间(WAT编译调用至JS方案)

Great question! Let's break this down step by step—first confirming your hunch about C++ and std::chrono in WebAssembly, then walking through how to implement this with WebAssembly Text Format (.wat) like you're asking.


First: Your C++/Chrono Speculation is Correct!

You’re right that you can compile C++ code using std::chrono to WebAssembly via Emscripten. Emscripten maps standard library time functions to JavaScript’s underlying high-precision time APIs. For example, when you call std::chrono::high_resolution_clock::now(), Emscripten translates that to use JavaScript’s performance.now() (or equivalent) under the hood. The resulting Wasm module will access high-resolution time, though the actual precision is tied to what the browser/JS runtime provides.


Implementing Nanosecond Time in .Wat (Text Format)

WebAssembly doesn’t have built-in instructions for accessing system time directly—you need to import a time-providing function from the host (JavaScript). Here’s a complete, working example:

Step 1: Write the .Wat Module

Create a file named time_module.wat with this code:

(module
  ;; Import a host function named `getNanoTime` from the `env` namespace
  ;; Takes no arguments, returns a 64-bit integer (ideal for nanosecond timestamps)
  (import "env" "getNanoTime" (func $getNanoTime (result i64)))

  ;; Export a Wasm function that wraps the imported time function
  (func (export "getCurrentNanoTime") (result i64)
    call $getNanoTime
  )
)

This module imports a JS-provided time function, then exports a wrapper you can call directly from JavaScript.

Step 2: Compile .Wat to .Wasm

Use the wat2wasm tool (part of the WebAssembly Binary Toolkit) to compile the text format to binary:

wat2wasm time_module.wat -o time_module.wasm

Step 3: Call the Wasm Function from JavaScript

In your JS code, define the imported time function, load the Wasm module, and invoke the exported function:

// Define the host function that converts JS high-precision time to nanoseconds
const importObject = {
  env: {
    getNanoTime: () => {
      // performance.now() returns milliseconds with sub-millisecond precision
      // Multiply by 1e6 to get nanoseconds, use BigInt to avoid floating-point loss
      return BigInt(Math.round(performance.now() * 1000000));
    }
  }
};

// Load and run the Wasm module
async function loadTimeWasm() {
  const response = await fetch('time_module.wasm');
  const bytes = await response.arrayBuffer();
  const { instance } = await WebAssembly.instantiate(bytes, importObject);
  
  // Call the exported Wasm function
  const nanoTimestamp = instance.exports.getCurrentNanoTime();
  console.log(`Relative nanosecond timestamp: ${nanoTimestamp}`);

  // If you need an absolute epoch timestamp (instead of relative), use this in getNanoTime:
  // return BigInt(Date.now() * 1000000) + BigInt(Math.round(performance.now() % 1 * 1000000));
}

loadTimeWasm();

Key Notes

  • Precision: While we’re returning "nanoseconds", the actual resolution is limited by the browser’s performance.now() implementation (usually microsecond-level in modern browsers). This is the highest precision available in the JS runtime.
  • 64-bit Integers: We use Wasm’s i64 type because nanosecond timestamps easily exceed 32-bit integer limits. JavaScript handles BigInts seamlessly with Wasm’s 64-bit values.
  • Emscripten C++ Alternative (Quick Example): If you’d rather stick with C++ instead of writing Wat, here’s a snippet:
    #include <chrono>
    #include <cstdint>
    
    extern "C" {
      uint64_t get_current_nano_time() {
        auto now = std::chrono::high_resolution_clock::now();
        auto duration = now.time_since_epoch();
        return std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count();
      }
    }
    
    Compile with Emscripten:
    emcc time.cpp -o time.js -s EXPORTED_FUNCTIONS='["_get_current_nano_time"]' -s EXPORTED_RUNTIME_METHODS='["ccall"]'
    
    Call from JS:
    const nanoTime = Module.ccall('get_current_nano_time', 'number', [], []);
    

内容的提问来源于stack exchange,提问作者cinnamonAsh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:49:05