Boosting Template Rendering 10×: Replace String.replace with StringBuilder

By analyzing the Alipay card‑package template substitution logic, the author identified costly String.replace calls and iteratively applied five optimization versions—removing indexOf/substring, adding caching, and finally substituting StringBuilder for String.replace—achieving over tenfold performance gains and reduced resource consumption.

Alibaba Cloud Developer
Alibaba Cloud Developer
Alibaba Cloud Developer
Boosting Template Rendering 10×: Replace String.replace with StringBuilder

Background

Alipay Card Pack stores users' membership cards and coupons. Both the card cell and card details are rendered by combining a static template with dynamic data, which is then presented to the end user.

Problem Discovery

The original template variable replacement logic iterates over the template string, using indexOf and substring to replace variables delimited by $. This approach is executed on every request and was suspected to have performance issues.

Performance Optimization V1

Removed the repeated indexOf and substring calls. The new method first extracts all variables from the template using a double‑pointer scan, stores them in a collection, and then replaces each variable in a single pass.

Performance Optimization V2

Introduced a local cache (using Google Guava) to store the mapping between a template ID and its variable set, eliminating the need to extract variables on every request and reducing pressure on the backend database.

Performance Optimization V3

Replaced String.replace with StringBuilder to avoid template recompilation and object creation for each replacement.

Performance Optimization V4

Combined the cache‑free approach of V3 with StringBuilder, achieving the highest speed gain (over 10×) while keeping the code relatively simple.

Performance Optimization V5

Removed the cache dependency, kept the variable extraction, and used StringBuilder to replace String.replace, resulting in a readable implementation with excellent performance.

Performance Comparison

Benchmarks show that V4 > V3 > V5 > V2 > V1 > original version. The dominant bottleneck is the String.replace call; replacing it with StringBuilder yields more than tenfold speed improvement, while caching provides additional but diminishing gains.

Conclusion

The key takeaways are that String.replace incurs template compilation and creates many temporary strings, consuming CPU and memory, while StringBuilder avoids these costs. Caching template‑variable mappings can further reduce load, but the most effective optimization is replacing String.replace with StringBuilder.

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.

JavaPerformance OptimizationcachingTemplate Renderingstringbuilder
Alibaba Cloud Developer
Written by

Alibaba Cloud Developer

Alibaba's official tech channel, featuring all of its technology innovations.

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.