function simpleStringify(o) {
let cache = [];
let data = JSON.stringify(o, function(key, value) {
if (typeof value === "object" && value !== null) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, discard key
return;
}
// Store value in our collection
cache.push(value);
}
return value;
});
cache = null;
return data;
}
var testData = [
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
{"bar":"foo"},
]
simpleStringify(testData);
JSON.stringify(testData)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
simpleStringify | |
JSON.stringify |
Test name | Executions per second |
---|---|
simpleStringify | 39700.9 Ops/sec |
JSON.stringify | 106793.9 Ops/sec |
Let's dive into the world of MeasureThat.net and explore what's being tested in this benchmark.
What is being tested?
The provided JSON represents two test cases:
simpleStringify(testData)
: This test case measures the performance of a custom JavaScript function, simpleStringify
, which takes an object as input and returns its string representation. The twist is that it also keeps track of circular references to optimize the stringification process.JSON.stringify(testData)
: This test case measures the performance of the built-in JSON.stringify()
method from the ECMAScript standard.Options being compared
In this benchmark, two options are being compared:
Pros and Cons
Custom implementation (simpleStringify):
Pros:
Cons:
Built-in implementation (JSON.stringify):
Pros:
Cons:
Other considerations
When evaluating these two options, consider the following factors:
Special JS features or syntax
There are no special JS features or syntax mentioned in this benchmark. However, it's worth noting that the JSON.stringify()
method has some nuances, such as:
Alternatives
If you're interested in exploring alternative stringification methods or optimizing performance in JavaScript, consider the following:
lodash.stringify()
or json-stringify-safe()
.JSON.stringify()
and its optional configuration options (e.g., reviver
) to optimize performance.Keep in mind that these alternatives might not provide the same level of optimization as the custom implementation in this benchmark.