소스 검색

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 :^)
Andreas Kling 1 년 전
부모
커밋
008b9f4c9f
1개의 변경된 파일4개의 추가작업 그리고 0개의 파일을 삭제
  1. 4 0
      Userland/Libraries/LibJS/Runtime/Value.cpp

+ 4 - 0
Userland/Libraries/LibJS/Runtime/Value.cpp

@@ -587,6 +587,10 @@ ThrowCompletionOr<NonnullGCPtr<Object>> Value::to_object(VM& vm) const
 // 7.1.3 ToNumeric ( value ), https://tc39.es/ecma262/#sec-tonumeric
 FLATTEN ThrowCompletionOr<Value> 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));