Skip to content

Commit fb81688

Browse files
committed
Refine the jsonResponse bench a bit more
Profile is dominated by the cost of constructing the object to serialize rather than the cost of the Response::json_ call itself.
1 parent ebc9c18 commit fb81688

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/workerd/tests/bench-response.c++

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ BENCHMARK_F(Response, arrayBufferBody)(benchmark::State& state) {
8888
BENCHMARK_F(Response, jsonResponse)(benchmark::State& state) {
8989
fixture->runInIoContext([&](const TestFixture::Environment& env) {
9090
auto& js = env.js;
91+
// Prepare object to serialize. Do this outside the loop to avoid measuring its repeated
92+
// construction cost. What we want to measure is just the cost of the api::Response::json_ call.
93+
auto obj = js.obj();
94+
obj.set(js, "key"_kj, js.str("value"_kj));
9195
for (auto _: state) {
92-
auto obj = js.obj();
93-
obj.set(js, js.str("key"_kj), js.str("value"_kj));
9496
benchmark::DoNotOptimize(api::Response::json_(js, obj, kj::none));
9597
}
9698
});

0 commit comments

Comments
 (0)