Skip to content

Commit

Permalink
Efficiently retrieve array length for JSI
Browse files Browse the repository at this point in the history
Summary:
When the target is known to be a `JSArray`, we can retrieve the length
without needing to actually get the length property.

Reviewed By: fbmal7

Differential Revision: D47638903

fbshipit-source-id: e2f9fbd53808f99f955562cac5042db82dfef4a8
  • Loading branch information
neildhar authored and facebook-github-bot committed Jul 27, 2023
1 parent bccfdbc commit 475698d
Showing 1 changed file with 1 addition and 12 deletions.
13 changes: 1 addition & 12 deletions API/hermes/hermes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1996,8 +1996,7 @@ jsi::ArrayBuffer HermesRuntimeImpl::createArrayBuffer(
}

size_t HermesRuntimeImpl::size(const jsi::Array &arr) {
vm::GCScope gcScope(runtime_);
return getLength(arrayHandle(arr));
return vm::JSArray::getLength(*arrayHandle(arr), runtime_);
}

size_t HermesRuntimeImpl::size(const jsi::ArrayBuffer &arr) {
Expand Down Expand Up @@ -2274,16 +2273,6 @@ vm::HermesValue HermesRuntimeImpl::stringHVFromUtf8(
return *strRes;
}

size_t HermesRuntimeImpl::getLength(vm::Handle<vm::ArrayImpl> arr) {
auto res = vm::JSObject::getNamed_RJS(
arr, runtime_, vm::Predefined::getSymbolID(vm::Predefined::length));
checkStatus(res.getStatus());
if (!(*res)->isNumber()) {
throw jsi::JSError(*this, "getLength: property 'length' is not a number");
}
return static_cast<size_t>((*res)->getDouble());
}

size_t HermesRuntimeImpl::getByteLength(vm::Handle<vm::JSArrayBuffer> arr) {
auto res = vm::JSObject::getNamed_RJS(
arr, runtime_, vm::Predefined::getSymbolID(vm::Predefined::byteLength));
Expand Down

0 comments on commit 475698d

Please sign in to comment.