Skip to content

Commit 7c52fb0

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 b59e107 commit 7c52fb0

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,12 @@ 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+
kj::StringPtr keys[] = {"key"_kj};
94+
jsg::JsValue values[] = {env.js.str("value"_kj)};
95+
auto obj = js.obj(kj::ArrayPtr(keys), kj::ArrayPtr(values));
9196
for (auto _: state) {
92-
auto obj = js.obj();
93-
obj.set(js, js.str("key"_kj), js.str("value"_kj));
9497
benchmark::DoNotOptimize(api::Response::json_(js, obj, kj::none));
9598
}
9699
});

0 commit comments

Comments
 (0)