const a = {}; for (let i = 0; i < 100000; i++) { a[i] = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; }
JSON.stringify(a);
const a = {}; for (let i = 0; i < 100000; i++) { a[i] = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; }
a[0] = a;
try {
JSON.stringify(a);
} catch (err) {
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
1 | |
2 |
Test name | Executions per second |
---|---|
1 | 76.5 Ops/sec |
2 | 743.1 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
What is being tested?
The provided benchmark tests two different approaches to creating circular references in objects, which are then passed to JSON.stringify()
. The goal is to determine whether the speed of JSON stringification changes with object size.
Options compared:
There are two test cases:
a
) with 100,000 properties, each initialized with a long string value. Then, it calls JSON.stringify(a)
.a[0] = a
). This causes a JavaScript error, which is caught and ignored.Pros and Cons of each approach:
Test Case 1 (no circular reference):
JSON.stringify()
when dealing with circular references.Test Case 2 (with circular reference):
JSON.stringify()
handles circular references, which can lead to performance issues in some cases.Library usage:
There is no explicit library used in these benchmark definitions. However, it's worth noting that JSON.stringify()
uses the built-in JSON object and its internal algorithms to serialize objects.
Special JS feature or syntax:
There are a few features being used here:
a
) with 100,000 properties.a[0] = a
). This is not a standard JavaScript feature but rather a creative way to test JSON.stringify()
behavior.Other alternatives:
If you'd like to explore alternative approaches or variations on this benchmark, here are some ideas:
JSON.stringify()
options, such as the replacer
and space
parameters.Keep in mind that these variations will likely require adjustments to the benchmark definitions and may not be directly comparable to the original test cases.