const a = { alpay: 5 }; JSON.stringify(a);
const a = {}; for (let i = 0; i < 10000; i++) { a[i] = {bannerImage : "https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx21-YCDoj1EkAxFn.jpg", id : "21", path : "", posterImage : "https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx21-YCDoj1EkAxFn.jpg", serverId : "http%3A%2F%2F192.168.0.249%3A10443%2F", title : "ONE PIECE"}; } JSON.stringify(a);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
1{bannerImage : "https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx21-YCDoj1EkAxFn.jpg", id : "21", path : "", posterImage : "https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx21-YCDoj1EkAxFn.jpg", serverId : "http%3A%2F%2F192.168.0.249%3A10443%2F", title : "ONE PIECE"} | |
2 |
Test name | Executions per second |
---|---|
1{bannerImage : "https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx21-YCDoj1EkAxFn.jpg", id : "21", path : "", posterImage : "https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx21-YCDoj1EkAxFn.jpg", serverId : "http%3A%2F%2F192.168.0.249%3A10443%2F", title : "ONE PIECE"} | 19903198.0 Ops/sec |
2 | 321.4 Ops/sec |
The benchmark described tests the performance of JSON.stringify
in JavaScript by comparing the speed of converting JavaScript objects of varying sizes into JSON string format. The purpose of this benchmark is to understand how the size of the object affects the speed of serialization, which helps developers make informed decisions when handling data representation in their applications.
Test Case 1:
const a = { alpay: 5 }; JSON.stringify(a);
alpay
). The benchmark measures how quickly this object can be converted into a JSON string.Test Case 2:
const a = {};
for (let i = 0; i < 10000; i++) {
a[i] = {bannerImage : "https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx21-YCDoj1EkAxFn.jpg", id : "21", path : "", posterImage : "https://s4.anilist.co/file/anilistcdn/media/anime/cover/large/bx21-YCDoj1EkAxFn.jpg", serverId : "http%3A%2F%2F192.168.0.249%3A10443%2F", title : "ONE PIECE"};
}
JSON.stringify(a);
JSON.stringify
handles larger, more complex objects.Pros of JSON.stringify
:
JSON.stringify
is a built-in method, which means it is well-supported across all modern browsers.Cons:
JSON.stringify
can exhibit significant performance degradation when converting complex objects with many properties.When handling large datasets or complex objects, there are several alternative approaches:
Custom Serialization: Implementing a custom serialization function can allow for optimization based on specific object structures, potentially enhancing speed and reducing bloat.
Using Libraries:
json-stringify-safe
can help with JSON serialization by handling circular references and offering additional configuration options.Flatted
is another alternative that can serialize and deserialize circular structures, which might be useful if the object graphs are complex.Other Data Formats: For very large datasets, you might consider alternatives to JSON, such as Protocol Buffers or MessagePack, which can offer better performance and support for binary formats.
This benchmark provides valuable insights into the performance characteristics of JSON.stringify
when serializing both small and large JavaScript objects. Understanding the speed trade-offs helps developers optimize their applications, particularly when dealing with large data structures that need to be serialized for APIs or storage. By considering alternatives and custom solutions, developers can further improve performance in their specific use cases.