Closed3

javascriptとwasmのブラウザ上でのコンパイル速度を比較してみる。

higumachanhigumachan
async function measureWasmParseTime(wasmFilePath) {
    // 1. wasm ファイルをバイナリデータとして読み込む
    const response = await fetch(wasmFilePath);
    const wasmBinary = await response.arrayBuffer();

    // 2. wasm のパース前のタイムスタンプを取得する
    const startTime = performance.now();

    // 3. `WebAssembly.compile()` を使って wasm バイナリデータをコンパイルする
    await WebAssembly.compile(wasmBinary);

    // 4. wasm のパース後のタイムスタンプを取得する
    const endTime = performance.now();

    // 5. 2 つのタイムスタンプの差分を計算することで、wasm のパース時間を得る
    const parseTime = endTime - startTime;
    console.log("Wasm parse time:", parseTime, "ms");
}

(async () => {
    const scriptElement = document.getElementById("myScript");
    const p = new Promise((resolve) => {
        scriptElement.addEventListener("load", () => {
            const performanceEntries = performance.getEntriesByName("https://cdn.jsdelivr.net/npm/dayjs@1/dayjs.min.js", "resource");
            const entry = performanceEntries[performanceEntries.length - 1];

            console.log("JS Script(dayjs) parse time:", entry.duration, "ms");
            resolve();
        });

        scriptElement.src = "https://cdn.jsdelivr.net/npm/dayjs@1/dayjs.min.js";
    });
    await p;


    await measureWasmParseTime("./da102741101b1e5f2ae5.module.wasm");
})();

というコードで比較してみるとかなり違う。

ただ、コード的に評価じゃなくてダウンロード時間も入っていそうなので調査する

higumachanhigumachan

やっぱり、ダウンロード時間も含まれているので上の計測結果は大嘘になってる。

このスクラップは5ヶ月前にクローズされました