1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 04:37:34 +00:00

LibJS: Avoid unnecessary ToObject conversion when resolving references

When performing GetValue on a primitive type we do not need to perform
the ToObject conversion as it will resolve to a property on the
prototype object.

To avoid this we skip the initial ToObject conversion on the base value
as it only serves to get the primitive's boxed prototype. We further
specialize on PrimitiveString in order to get efficient behaviour
behaviour for the direct properties.

Depending on the tests anywhere from 20 to 60%, with significant loop
overhead.
This commit is contained in:
Anonymous 2022-02-12 00:48:23 -08:00 committed by Andreas Kling
parent 3a184f7841
commit d1cc67bbe1
7 changed files with 77 additions and 24 deletions

View file

@ -4,9 +4,13 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "LibJS/Runtime/Value.h"
#include <AK/CharacterTypes.h>
#include <AK/Utf16View.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/PrimitiveString.h>
#include <LibJS/Runtime/PropertyKey.h>
#include <LibJS/Runtime/VM.h>
namespace JS {
@ -51,6 +55,26 @@ Utf16View PrimitiveString::utf16_string_view() const
return utf16_string().view();
}
Optional<Value> PrimitiveString::get(GlobalObject& global_object, PropertyKey const& property_key) const
{
if (property_key.is_symbol())
return {};
if (property_key.is_string()) {
if (property_key.as_string() == global_object.vm().names.length.as_string()) {
auto length = utf16_string().length_in_code_units();
return Value(static_cast<double>(length));
}
}
auto index = canonical_numeric_index_string(property_key);
if (!index.has_value())
return {};
auto str = utf16_string_view();
auto length = str.length_in_code_units();
if (length <= *index)
return {};
return js_string(vm(), str.substring_view(*index, 1));
}
PrimitiveString* js_string(Heap& heap, Utf16View const& view)
{
return js_string(heap, Utf16String(view));