From eda8d563eaaad4f702c829db9ba2713214f2ca89 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 3 Apr 2025 12:45:25 +0200 Subject: [PATCH] LibJS: Add fast paths in ToNumeric for booleans, null and undefined These are still in the out-of-line "slow path" for ToNumeric, but skipping all the coercion machinery can save us a lot of time. --- Libraries/LibJS/Runtime/Value.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Libraries/LibJS/Runtime/Value.cpp b/Libraries/LibJS/Runtime/Value.cpp index bdc464ee781..a9e6399a3a1 100644 --- a/Libraries/LibJS/Runtime/Value.cpp +++ b/Libraries/LibJS/Runtime/Value.cpp @@ -602,6 +602,17 @@ ThrowCompletionOr> Value::to_object(VM& vm) const // 7.1.3 ToNumeric ( value ), https://tc39.es/ecma262/#sec-tonumeric FLATTEN ThrowCompletionOr Value::to_numeric_slow_case(VM& vm) const { + // OPTIMIZATION: Fast paths for some trivial common cases. + if (is_boolean()) { + return Value(as_bool() ? 1 : 0); + } + if (is_null()) { + return Value(0); + } + if (is_undefined()) { + return js_nan(); + } + // 1. Let primValue be ? ToPrimitive(value, number). auto primitive_value = TRY(to_primitive(vm, Value::PreferredType::Number));