Repository: electron/electron
File: shell/renderer/api/electron_api_context_bridge.cc (C++)

Code before:
  if (source_execution_context->IsWindow()) {
    // Custom logic to "clone" Element references
    blink::WebElement elem = blink::WebElement::FromV8Value(
        destination_context->GetIsolate(), value);
    if (!elem.IsNull()) {
      v8::Context::Scope destination_context_scope(destination_context);
      return v8::MaybeLocal<v8::Value>(
          elem.ToV8Value(destination_context->GetIsolate()));
    }

    // Custom logic to "clone" Blob references
    blink::WebBlob blob =
        blink::WebBlob::FromV8Value(destination_context->GetIsolate(), value);
    if (!blob.IsNull()) {
      v8::Context::Scope destination_context_scope(destination_context);
      return v8::MaybeLocal<v8::Value>(
          blob.ToV8Value(destination_context->GetIsolate()));
    }

    // Custom logic to "clone" FileList references
    auto object_value = value.As<v8::Object>();
    v8::Maybe<bool> has_length = object_value->Has(
        source_context,
        gin::StringToV8(source_context->GetIsolate(), "length"));
    v8::Maybe<bool> has_item = object_value->Has(
        source_context, gin::StringToV8(source_context->GetIsolate(), "item"));
    if (has_length.FromJust() && has_item.FromJust()) {
      v8::Local<v8::Value> length, item_fn;
      if (object_value
              ->Get(source_context,
                    gin::StringToV8(source_context->GetIsolate(), "length"))
              .ToLocal(&length) &&
          length->IsNumber() &&
          object_value
              ->Get(source_context,
                    gin::StringToV8(source_context->GetIsolate(), "item"))
              .ToLocal(&item_fn) &&
          item_fn->IsFunction()) {
        v8::Context::Scope destination_scope(destination_context);
        uint32_t item_count = length->Uint32Value(source_context).FromJust();
        v8::Local<v8::Array> array =
            v8::Array::New(destination_context->GetIsolate(), item_count);

        for (uint32_t i = 0; i < item_count; i++) {
          v8::Local<v8::Value> file;
          if (object_value->Get(source_context, i).ToLocal(&file)) {
            auto passed_file = PassValueToOtherContextInner(
                source_context, source_execution_context, destination_context,
                file, value, object_cache, support_dynamic_properties,
                recursion_depth + 1, error_target);


Review comment (suggestion):
- When you've got a string literal, `v8::String::NewFromUtf8Literal()` is fastest
- We v8ize these two keys twice, but only need to do it once.

```suggestion

    const v8::Local<v8::String> length_key =
        v8::String::NewFromUtf8Literal(source_context->GetIsolate(), "length");
    const v8::Local<v8::String> item_key =
        v8::String::NewFromUtf8Literal(source_context->GetIsolate(), "item");

    v8::Maybe<bool> has_length = object_value->Has(source_context, length_key);
    v8::Maybe<bool> has_item = object_value->Has(source_context, item_key);
```

Code after:
  if (source_execution_context->IsWindow()) {
    // Custom logic to "clone" Element references
    blink::WebElement elem = blink::WebElement::FromV8Value(
        destination_context->GetIsolate(), value);
    if (!elem.IsNull()) {
      v8::Context::Scope destination_context_scope(destination_context);
      return v8::MaybeLocal<v8::Value>(
          elem.ToV8Value(destination_context->GetIsolate()));
    }

    // Custom logic to "clone" Blob references
    blink::WebBlob blob =
        blink::WebBlob::FromV8Value(destination_context->GetIsolate(), value);
    if (!blob.IsNull()) {
      v8::Context::Scope destination_context_scope(destination_context);
      return v8::MaybeLocal<v8::Value>(
          blob.ToV8Value(destination_context->GetIsolate()));
    }
  }

  // Proxy all objects
  if (IsPlainObject(value)) {
    auto object_value = value.As<v8::Object>();
    auto passed_value = CreateProxyForAPI(
        object_value, source_context, source_execution_context,
        destination_context, object_cache, support_dynamic_properties,
        recursion_depth + 1, error_target);
    if (passed_value.IsEmpty())
      return {};
    return v8::MaybeLocal<v8::Value>(passed_value.ToLocalChecked());
  }

  // Serializable objects
  blink::CloneableMessage ret;
  {
    v8::Local<v8::Context> error_context =
        error_target == BridgeErrorTarget::kSource ? source_context
                                                   : destination_context;
    v8::Context::Scope error_scope(error_context);
    // V8 serializer will throw an error if required
    if (!gin::ConvertFromV8(error_context->GetIsolate(), value, &ret)) {
      return {};
    }
  }

  {
    v8::Context::Scope destination_context_scope(destination_context);
    v8::Local<v8::Value> cloned_value =
        gin::ConvertToV8(destination_context->GetIsolate(), ret);
    object_cache->CacheProxiedObject(value, cloned_value);
    return v8::MaybeLocal<v8::Value>(cloned_value);