Skip to content

Instantly share code, notes, and snippets.

@bmeurer
Created January 2, 2022 13:19
Show Gist options
  • Save bmeurer/5693f8938888b97ac640189d53aaa6fe to your computer and use it in GitHub Desktop.
Save bmeurer/5693f8938888b97ac640189d53aaa6fe to your computer and use it in GitHub Desktop.
Standalone d8 performance test for Runtime.setMaxCallStackSizeToCapture
// Copyright (c) 2022 Benedikt Meurer. All rights reserved.
//
// Run this test with
//
// d8 --enable-inspector standalone.js -- -1
//
// to measure with the inspector disabled, or with
//
// d8 --enable-inspector standalone.js -- 0
//
// to explicitly disable inspector stack trace collection via
// Runtime.setMaxCallStackSizeToCapture({size:0}), or via
//
// d8 --enable-inspector standalone.js -- 200
//
// to test the default DevTools behavior.
//
// See the design document at
//
// https://bit.ly/v8-cheaper-inspector-stack-traces
//
// for more context.
//
function recurse(n, f) {
if (n > 0) return recurse(n - 1, f);
return f();
}
function test(d = 64, n = 8 * 1024) {
const startTime = performance.now();
recurse(d, () => {
while (n-- > 0) {
new Error();
}
});
const endTime = performance.now();
return endTime - startTime;
}
const inspector = (() => {
const requests = [];
globalThis.receive = message => {
message = JSON.parse(message);
if ('id' in message) {
try {
const resolve = requests[message.id];
delete requests[message.id];
resolve(message.result);
} catch (e) {
print(e.stack);
}
}
}
return {
send(method, params = {}) {
return new Promise(resolve => {
const id = requests.length;
requests.push(resolve);
globalThis.send(JSON.stringify({id, method, params}));
});
}
};
})();
const size = arguments.length > 0 ? parseInt(arguments[0]) : -1;
const depth = arguments.length > 1 ? parseInt(arguments[1]) : 128;
const stacks = arguments.length > 2 ? parseInt(arguments[2]) : 8 * 1024;
print(`Configuration: size=${size}, depth=${depth}, stacks=${stacks}`);
Promise.all(size < 0 ? [] : [
inspector.send('Runtime.enable'),
inspector.send('Runtime.setMaxCallStackSizeToCapture', {size}),
]).then(() => {
for (let i = 0; i < 10; ++i) test(depth, stacks);
let overall = 0;
for (let i = 0; i < 10; ++i) {
const time = test(depth, stacks);
print(`Time: ${time.toFixed(2)}ms.`);
overall += time;
}
print(`Average: ${(overall / 10).toFixed(2)}ms.`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment