From 008b9f4c9feeff43c48a1393570eb2b2ee87fdee Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 3 Sep 2023 18:07:27 +0200 Subject: [PATCH] LibJS: Add fast-path for Value::to_numeric() on number values We can skip a whole bunch of checks in this case and just return the value itself. 2% speed-up on Kraken/imaging-darkroom.js :^) --- Userland/Libraries/LibJS/Runtime/Value.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index 42147d823d..4cc787169d 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -587,6 +587,10 @@ ThrowCompletionOr> Value::to_object(VM& vm) const // 7.1.3 ToNumeric ( value ), https://tc39.es/ecma262/#sec-tonumeric FLATTEN ThrowCompletionOr Value::to_numeric(VM& vm) const { + // OPTIMIZATION: Fast path for when this value is already a number. + if (is_number()) + return *this; + // 1. Let primValue be ? ToPrimitive(value, number). auto primitive_value = TRY(to_primitive(vm, Value::PreferredType::Number));