LibJS: Cache the Intl.Collator in String.prototype.localeCompare()

In the very common case that no special constructor options are provided
for the Intl.Collator when calling localeCompare() on a string, we can
cache and reuse a default-constructed Intl.Collator, saving lots of time
and space.

This shaves a fair bit of load time off of https://wpt.fyi/ where they
use Array.prototype.sort() and localeCompare() to sort a big JSON thing.

Time spent in sort():
- Before: 1656 ms
- After: 135 ms
This commit is contained in:
Andreas Kling 2025-01-23 18:20:29 +01:00 committed by Andreas Kling
commit d465e2aa2b
Notes: github-actions[bot] 2025-01-23 20:39:21 +00:00
3 changed files with 26 additions and 1 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2020-2025, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -95,6 +96,8 @@ public:
JS_ENUMERATE_ITERATOR_PROTOTYPES
#undef __JS_ENUMERATE
[[nodiscard]] GC::Ref<Intl::Collator> default_collator();
private:
Intrinsics(Realm& realm)
: m_realm(realm)
@ -189,6 +192,8 @@ private:
GC::Ptr<Object> m_##snake_name##_prototype;
JS_ENUMERATE_ITERATOR_PROTOTYPES
#undef __JS_ENUMERATE
GC::Ptr<Intl::Collator> m_default_collator;
};
void add_restricted_function_properties(FunctionObject&, Realm&);