Skip to content

Commit

Permalink
LibJS: Optimize & Bring String.prototype.repeat closer to the spec
Browse files Browse the repository at this point in the history
Specifically, we immediately return an empty string when `this` is an
empty string, instead of wasting time in a loop doing nothing N times.
  • Loading branch information
IdanHo authored and linusg committed Jun 30, 2021
1 parent 5f09d78 commit 7d055dd
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions Userland/Libraries/LibJS/Runtime/StringPrototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,30 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::repeat)
auto string = ak_string_from(vm, global_object);
if (!string.has_value())
return {};
if (!vm.argument_count())
return js_string(vm, String::empty());
auto count = vm.argument(0).to_integer_or_infinity(global_object);

auto n = vm.argument(0).to_integer_or_infinity(global_object);
if (vm.exception())
return {};
if (count < 0) {

if (n < 0) {
vm.throw_exception<RangeError>(global_object, ErrorType::StringRepeatCountMustBe, "positive");
return {};
}
if (Value(count).is_infinity()) {

if (Value(n).is_positive_infinity()) {
vm.throw_exception<RangeError>(global_object, ErrorType::StringRepeatCountMustBe, "finite");
return {};
}

if (n == 0)
return js_string(vm, String::empty());

// NOTE: This is an optimization, it is not required by the specification but it produces equivalent behaviour
if (string->is_empty())
return js_string(vm, String::empty());

StringBuilder builder;
for (size_t i = 0; i < count; ++i)
for (size_t i = 0; i < n; ++i)
builder.append(*string);
return js_string(vm, builder.to_string());
}
Expand Down

0 comments on commit 7d055dd

Please sign in to comment.