From 7b30df084048541e6c850d7c4fbe9b40d94a24ac Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 10 Nov 2022 20:36:02 +0100 Subject: [PATCH] LibJS: Make ObjectEnvironment::set_mutable_binding() faster as well We can combine HasProperty and Set into just Set in non-strict mode for non-with object environments. --- Userland/Libraries/LibJS/Runtime/ObjectEnvironment.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Userland/Libraries/LibJS/Runtime/ObjectEnvironment.cpp b/Userland/Libraries/LibJS/Runtime/ObjectEnvironment.cpp index 2ff649c32c..11ee6986c5 100644 --- a/Userland/Libraries/LibJS/Runtime/ObjectEnvironment.cpp +++ b/Userland/Libraries/LibJS/Runtime/ObjectEnvironment.cpp @@ -92,6 +92,13 @@ ThrowCompletionOr ObjectEnvironment::set_mutable_binding(VM&, FlyString co { auto& vm = this->vm(); + // OPTIMIZATION: For non-with environments in non-strict mode, we don't need the separate HasProperty check since we only use that + // information to throw errors in strict mode. + // We can't do this for with environments, since it would be observable (e.g via a Proxy) + // FIXME: I think we could combine HasProperty and Set in strict mode if Set would return a bit more failure information. + if (!m_with_environment && !strict) + return m_binding_object.set(name, value, Object::ShouldThrowExceptions::No); + // 1. Let bindingObject be envRec.[[BindingObject]]. // 2. Let stillExists be ? HasProperty(bindingObject, N). auto still_exists = TRY(m_binding_object.has_property(name));