LibJS: Add cache for the string "[object Object]"

This is very frequently returned by Object.prototype.toString(),
so we benefit from caching it instead of recreating it every time.

Takes Speedometer2.1/EmberJS-Debug-TodoMVC from ~4000ms to ~3700ms
on my M3 MacBook Pro.
This commit is contained in:
Andreas Kling 2025-04-14 23:42:31 +02:00 committed by Andreas Kling
commit 81d4ed27d8
Notes: github-actions[bot] 2025-04-15 11:09:47 +00:00
4 changed files with 27 additions and 20 deletions

View file

@ -326,38 +326,38 @@ GC::Ref<PrimitiveString> Value::typeof_(VM& vm) const
{
// 9. If val is a Number, return "number".
if (is_number())
return *vm.typeof_strings.number;
return *vm.cached_strings.number;
switch (m_value.tag) {
// 4. If val is undefined, return "undefined".
case UNDEFINED_TAG:
return *vm.typeof_strings.undefined;
return *vm.cached_strings.undefined;
// 5. If val is null, return "object".
case NULL_TAG:
return *vm.typeof_strings.object;
return *vm.cached_strings.object;
// 6. If val is a String, return "string".
case STRING_TAG:
return *vm.typeof_strings.string;
return *vm.cached_strings.string;
// 7. If val is a Symbol, return "symbol".
case SYMBOL_TAG:
return *vm.typeof_strings.symbol;
return *vm.cached_strings.symbol;
// 8. If val is a Boolean, return "boolean".
case BOOLEAN_TAG:
return *vm.typeof_strings.boolean;
return *vm.cached_strings.boolean;
// 10. If val is a BigInt, return "bigint".
case BIGINT_TAG:
return *vm.typeof_strings.bigint;
return *vm.cached_strings.bigint;
// 11. Assert: val is an Object.
case OBJECT_TAG:
// B.3.6.3 Changes to the typeof Operator, https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot-typeof
// 12. If val has an [[IsHTMLDDA]] internal slot, return "undefined".
if (as_object().is_htmldda())
return *vm.typeof_strings.undefined;
return *vm.cached_strings.undefined;
// 13. If val has a [[Call]] internal slot, return "function".
if (is_function())
return *vm.typeof_strings.function;
return *vm.cached_strings.function;
// 14. Return "object".
return *vm.typeof_strings.object;
return *vm.cached_strings.object;
default:
VERIFY_NOT_REACHED();
}