Fundamentals 9 min read

Why Protocol Buffers Outperform JSON: A Hands‑On Java Benchmark

This article introduces Google’s Protocol Buffers, explains its compilation process, walks through a complete Java example, and compares its encoding speed, data size, and memory usage against JSON, showing that ProtoBuf becomes significantly faster and more compact as the number of operations increases.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Why Protocol Buffers Outperform JSON: A Hands‑On Java Benchmark

Introduction

ProtoBuf is a tool developed by Google for efficiently storing and reading structured data. Structured data refers to information that follows a defined schema, such as a phone‑book record containing name, ID, email, and phone number.

Similar Formats

XML and JSON can also store such structured data, but ProtoBuf representations are more efficient and result in smaller payloads.

Principle

ProtoBuf uses the protoc compiler to translate language‑agnostic *.proto definition files into language‑specific classes (Java, C/C++, Python, etc.). The generated classes are then used via Google‑provided runtime libraries.

ProtoBuf Compiler Installation

Mac:

brew install protobuf

Example

1. Create a .proto file

syntax = "proto3";

message Person {
    int32 id = 1;
    string name = 2;
    repeated Phone phone = 4;
    enum PhoneType {
        MOBILE = 0;
        HOME = 1;
        WORK = 2;
    }
    message Phone {
        string number = 1;
        PhoneType type = 2;
    }
}

2. Create a Java project

Place the .proto file under src/main/proto.

3. Compile the .proto file to Java

From the src/main directory run: protoc --java_out=./java ./proto/*.proto The corresponding Java classes are generated in src/main/java.

4. Add ProtoBuf Java library dependency (Gradle example)

implementation 'com.google.protobuf:protobuf-java:3.9.1'

5. Serialize a Java object to ProtoBuf

Message.Person.Phone.Builder phoneBuilder = Message.Person.Phone.newBuilder();
Message.Person.Phone phone1 = phoneBuilder.setNumber("100860").setType(Message.Person.PhoneType.HOME).build();
Message.Person.Phone phone2 = phoneBuilder.setNumber("100100").setType(Message.Person.PhoneType.MOBILE).build();
Message.Person.Builder personBuilder = Message.Person.newBuilder();
personBuilder.setId(1994).setName("XIAOLEI").addPhone(phone1).addPhone(phone2);
Message.Person person = personBuilder.build();
long start = System.currentTimeMillis();
byte[] buff = person.toByteArray();
System.out.println("ProtoBuf encoding time: " + (System.currentTimeMillis() - start));
System.out.println("ProtoBuf data length:" + buff.length);

6. Deserialize ProtoBuf data back to a Java object

System.out.println("-Start decoding-");
start = System.currentTimeMillis();
Message.Person personOut = Message.Person.parseFrom(buff);
System.out.println("ProtoBuf decoding time: " + (System.currentTimeMillis() - start));
System.out.printf("Id:%d, Name:%s
", personOut.getId(), personOut.getName());
for (Message.Person.Phone phone : personOut.getPhoneList()) {
    System.out.printf("Phone:%s (%s)
", phone.getNumber(), phone.getType());
}

Comparison with JSON

The same data structure was serialized to JSON using Google’s GSON library. The following benchmark results compare encoding time, data length, and decoding time for various iteration counts.

【 JSON start encoding 】
JSON encoding 1 time, cost: 22ms
JSON data length: 106
-Start decoding-
JSON decoding 1 time, cost: 1ms

【 ProtoBuf start encoding 】
ProtoBuf encoding 1 time, cost: 32ms
ProtoBuf data length: 34
-Start decoding-
ProtoBuf decoding 1 time, cost: 3ms

... (results for 10, 100, 1,000, 10,000, 100,000 iterations) ...

Summary

Encoding/Decoding Performance

For fewer than 1,000 operations, ProtoBuf performance is comparable to JSON and may even be slower.

Beyond 2,000 operations, ProtoBuf consistently outperforms JSON.

At 100,000+ operations, the performance gap becomes very pronounced.

Memory Usage

ProtoBuf occupies 34 bytes versus 106 bytes for JSON, roughly one‑third of the memory footprint.

Compatibility

Adding Fields

Add a new nickname field to the .proto file.

Regenerate Java classes.

Old byte arrays can still be parsed, with the new field defaulting to empty.

Id:1994, Name:XIAOLEI
Phone:100860 (HOME)
Phone:100100 (MOBILE)
getNickname=

Removing Fields

Remove the name field from the .proto file.

Regenerate Java classes.

Old byte arrays can still be parsed; the removed field becomes null.

Id:1994, Name:null
Phone:100860 (HOME)
Phone:100100 (MOBILE)

Even with this simple test, ProtoBuf’s advantages in speed and size are evident.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performanceserializationProtocol BuffersData Formats
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.