How we made JSON.stringify more than twice as fast
9 months ago
- #JavaScript
- #Performance
- #V8
- JSON.stringify in V8 is now more than twice as fast due to recent optimizations.
- A new fast path ensures side-effect-free serialization, bypassing expensive checks for common data objects.
- Specialized string serialization handles one-byte and two-byte characters efficiently with templatized implementations.
- SIMD and SWAR techniques accelerate string serialization by processing multiple characters at once.
- An 'express lane' optimizes serialization for objects with the same hidden class, skipping redundant checks.
- The Dragonbox algorithm replaces Grisu3 for faster number-to-string conversions, benefiting all string conversions in V8.
- A segmented buffer approach replaces the contiguous buffer, reducing memory overhead for large JSON objects.
- Limitations include no replacer or space arguments, plain data objects, no indexed properties, and simple string types.
- These optimizations are available in V8 starting with version 13.8 (Chrome 138).