Deltadga

5 Ways V8 Made JSON.stringify Twice as Fast (And What It Means for Your Code)

V8 made JSON.stringify twice as fast via a side-effect-free fast path, iterative traversal, templatized string handling, and more. Learn the five key optimizations.

Deltadga · 2026-05-03 20:05:48 · Web Development

JSON.stringify is a core JavaScript function for serializing data. Its performance directly affects common operations across the web, from serializing data for a network request to saving data to localStorage. A faster JSON.stringify translates to quicker page interactions and more responsive applications. That’s why the recent engineering effort in V8 that made JSON.stringify more than twice as fast is a big deal. In this article, we’ll break down the five key optimizations that made this improvement possible—and show you how they can benefit your everyday code.

1. A Side-Effect-Free Fast Path

The foundation of the speedup is a new fast path built on a simple premise: if V8 can guarantee that serializing an object will not trigger any side effects, it can use a much faster, specialized implementation. A “side effect” here is anything that breaks the simple, streamlined traversal of an object—including not only obvious cases like executing user-defined code during serialization (e.g., toJSON methods or custom getters), but also more subtle internal operations that might trigger a garbage collection cycle. For more details on what exactly can cause side effects and how you can avoid them, see item 5.

5 Ways V8 Made JSON.stringify Twice as Fast (And What It Means for Your Code)
Source: v8.dev

As long as V8 can determine that serialization will be free from these effects, it stays on this highly-optimized path. This allows it to bypass many expensive checks and defensive logic required by the general-purpose serializer, resulting in a significant speedup for the most common types of JavaScript objects that represent plain data. In practice, this means arrays of numbers, strings, or plain objects—the bread and butter of JSON—get a huge performance boost.

2. Iterative Instead of Recursive Traversal

The new fast path is iterative, in contrast to the recursive general-purpose serializer. This architectural choice eliminates the need for stack overflow checks and allows V8 to quickly resume after encoding changes. Recursive implementations can also hit call-stack limits when nesting is deep—a problem that the iterative approach gracefully avoids.

With the iterative method, developers can serialize significantly deeper nested object graphs than was previously possible. For example, deeply nested configuration objects or tree structures that once caused stack overflows now serialize without issue. This not only improves reliability but also maintains top speed because there’s no overhead from managing the call stack. The iterative fast path is a win-win for both performance and capability.

3. Templatized String Handling for One-Byte and Two-Byte Strings

Strings in V8 can be represented with either one-byte or two-byte characters. If a string contains only ASCII characters, it’s stored as a one-byte string using 1 byte per character. However, if it contains just a single character outside the ASCII range, all characters switch to a two-byte representation, doubling memory use. To avoid constant branching and type checks, the entire string serializer is now templatized on the character type.

This means V8 compiles two distinct, specialized versions of the serializer: one completely optimized for one-byte strings and another for two-byte strings. While this does increase binary size, the performance gains are well worth it. The two paths are streamlined for their respective character widths, eliminating runtime checks that would slow down a unified implementation. For the vast majority of strings in typical JSON (which are ASCII), this results in a noticeable speed boost.

4. Efficient Handling of Mixed Encodings

Even with the templatized approach, real-world data often contains a mixture of one-byte and two-byte strings. During serialization, V8 must inspect each string’s instance type to detect representations that can’t be handled on the fast path—like ConsString, which might trigger a garbage collection during flattening. This necessary check is now optimized to minimize overhead.

When a mixed-encoding situation is encountered (e.g., a mostly-ASCII string with a few non-ASCII characters), the serializer can still take advantage of the fast path for the one-byte parts and fall back to the slow path only when absolutely necessary. This hybrid strategy ensures that the performance benefit isn’t lost when dealing with international text or special characters. The result is a serializer that handles the messy reality of user-generated data without sacrificing speed.

5. Trade-Offs and Limitations to Know

No optimization comes without trade-offs. The new fast path works only when V8 can guarantee no side effects. If your objects have custom toJSON methods, getters that run code, or if they include BigInt values (which JSON doesn’t support natively), the serializer falls back to the slower general-purpose path. Additionally, the templatized string handling increases binary size slightly—though the V8 team considers this a worthwhile price for the speed gains.

To maximize the benefit, stick to simple, plain objects and arrays. Avoid defining custom toJSON unless necessary, and be mindful of Proxy objects or properties with getters that might trigger side effects. By following these guidelines, you’ll let V8 stay on the fast path and enjoy the full speed improvement in your applications.

Conclusion

The optimizations in JSON.stringify—a side-effect-free fast path, iterative traversal, templatized string handling, efficient mixed-encoding support, and clear limitations—add up to a more than twofold performance boost for the most common use cases. For developers, this means faster network requests, more responsive localStorage operations, and overall snappier JavaScript applications. By understanding these improvements, you can write code that stays on the fast path and leverage the full power of V8’s JSON serialization.

Recommended