Skip to content

Make images smaller using best-in-class codecs, right in the browser.

JavaScript

Write d8 benchmark using the baseline compiler

Surmadb20f10bd2b1
diff --git a/codecs/rotate/benchmark.js b/codecs/rotate/benchmark.js
new file mode 100644
index 00000000..d1551574
--- /dev/null
+++ b/codecs/rotate/benchmark.js
@@ -0,0 +1,37 @@
+// THIS IS NOT A NODE SCRIPT
+// This is a d8 script. Please install jsvu[1] and install v8.
+// Then run `npm run --silent benchmark`.
+// [1]: https://github.com/GoogleChromeLabs/jsvu
+async function init() {
+ // Adjustable constants.
+ const imageDimensions = 4096;
+ const iterations = new Array(100);
+
+ // Constants. Don’t change.
+ const imageByteSize = imageDimensions * imageDimensions * 4;
+ const wasmPageSize = 64 * 1024;
+
+ const buffer = readbuffer("rotate.wasm");
+ const {instance} = await WebAssembly.instantiate(buffer);
+
+ const pagesAvailable = Math.floor(instance.exports.memory.buffer.byteLength / wasmPageSize);
+ const pagesNeeded = Math.floor((imageByteSize * 2 + 4) / wasmPageSize) + 1;
+ const additionalPagesNeeded = pagesNeeded - pagesAvailable;
+ if(additionalPagesNeeded > 0) {
+ instance.exports.memory.grow(additionalPagesNeeded)
+ }
+
+ for(let i = 0; i < 100; i++) {
+ const start = Date.now();
+ instance.exports.rotate(imageDimensions, imageDimensions, 90);
+ iterations[i] = Date.now() - start;
+ }
+ const average = iterations.reduce((sum, c) => sum + c) / iterations.length;
+ const stddev = Math.sqrt(iterations
+ .map(i => Math.pow(i - average, 2))
+ .reduce((sum, c) => sum + c) / iterations.length);
+ print(`n = ${iterations.length}`);
+ print(`Average: ${average}`)
+ print(`StdDev: ${stddev}`);
+}
+init().catch(e => console.error(e.stack));
diff --git a/codecs/rotate/package.json b/codecs/rotate/package.json
index d7388aaa..b295b0f8 100644
--- a/codecs/rotate/package.json
+++ b/codecs/rotate/package.json
@@ -2,6 +2,7 @@
"name": "rotate",
"scripts": {
"build:image": "docker build -t squoosh-rotate .",
- "build": "docker run --rm -v $(pwd):/src squoosh-rotate ./build.sh"
+ "build": "docker run --rm -v $(pwd):/src squoosh-rotate ./build.sh",
+ "benchmark": "v8 --liftoff --no-wasm-tier-up --no-opt ./benchmark.js"
}
}