16

In the examples of the GRPC client there are two types of implementation, one where the .proto files are loaded and processed at runtime, and one where they are compiled using protoc.

My question is: what is the difference? The docs say nothing more than 'they behave identically', but surely there has to be a difference right?

Sander
  • 1,183
  • 12
  • 27

1 Answers1

14

Fundamentally, the primary difference is the one you mentioned: with the dynamic code generation, the .proto file is loaded and parsed at run time, and with static code generation, the .proto file is preprocessed into JavaScript.

The dynamic code generation is simpler to use, potentially easier to debug, and generates code that accepts regular JavaScript objects.

The static code generation (using protoc) requires the user to create protobuf objects, which means that input validation will be done earlier. It is also a workflow that is more consistent with other languages.

murgatroid99
  • 19,007
  • 10
  • 60
  • 95
  • 4
    Okay, so that makes it look like dynamic code generation would always be the way to go, right? I mean, why go through so much trouble if you can just drop `.proto` files in the grpc library and be done with it? I also noticed that you have to use the generated getters and setters when using the static code, which makes it a bit more cumbersome to use, so I see why the dynamic code would be easier. – Sander Apr 05 '17 at 07:38
  • 3
    @Sander As @murgatroid99 mentioned, one advantage of static code generation is input validation. Imagine your request has a field called `last_name`, and your client mistyped it as `lastName`. If using static generation, then this error will be detected before the request is even sent to the server, because the code knows that `lastName` is a nonexistent field. In contrast, if using dynamic code generation, such error may cause silent failures. That is, `last_name` will be left as its default value. – kgf3JfUtW Jul 31 '19 at 17:26
  • 5
    What is the difference in performance between the two methods? – Vipresh May 26 '20 at 10:46
  • Besides, if we use TypeScript, static code generation will provide the type safe stuffs such as types, interfaces. It will standardize your code. – Lin Du Aug 28 '20 at 08:17