<script>
window.exports = {};
</script>
<!-- Don't use fastest-json-copy like this. Instead, import it and use webpack. -->
<script src="https://cdn.jsdelivr.net/npm/fastest-json-copy@1.0.1/lib/v1.js"></script>
<script>
window.fastestJsonCopy = window.exports.copy;
</script>
var MyObject = {
a: 1,
b: 'ddddddddddddddddd',
c: 'sssssssssssssssssssssssssssssssssssssssssssssssssss',
d: [1, 2, 3, 4, 5, 6, 7, 8, 9],
};
var myCopy = null;
myCopy = JSON.parse(JSON.stringify(MyObject));
myCopy = fastestJsonCopy(MyObject);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
JSON.parse(JSON.stringify()) | |
fastest-json-copy npm package |
Test name | Executions per second |
---|---|
JSON.parse(JSON.stringify()) | 173871.7 Ops/sec |
fastest-json-copy npm package | 183229.0 Ops/sec |
Let's break down the provided benchmark and explain what is being tested.
Benchmark Overview
The benchmark measures the performance of two approaches for copying a JSON object in JavaScript:
JSON.parse(JSON.stringify(MyObject))
fastest-json-copy
npm package (fastestJsonCopy(MyObject)
)JSON.parse(JSON.stringify())
This approach uses the built-in JSON.parse()
and JSON.stringify()
methods to create a deep copy of the input JSON object MyObject
. This method is a part of the JavaScript Standard Library.
Pros:
Cons:
fastest-json-copy npm package
This approach uses a third-party library called fastest-json-copy
(imported via a CDN) to create a deep copy of the input JSON object MyObject
. The library provides a simple and efficient way to clone objects.
Pros:
Cons:
Library: fastest-json-copy
The fastest-json-copy
library is a pure JavaScript implementation that provides a simple and efficient way to clone objects. It's designed to outperform other methods for large-scale JSON copying.
Pros:
Cons:
Special JS Feature/Syntax
There is no special JavaScript feature or syntax used in this benchmark. The tests only rely on standard JavaScript functions and variables.
Other Alternatives
If you're looking for alternative methods to copy JSON objects, here are a few options:
slice()
method to create a shallow copy of an object's properties.Object.assign()
method to create a new object and assign values from the original object.cloneDeep()
function to create a deep copy of an object.Keep in mind that each approach has its pros and cons, and the choice ultimately depends on your specific use case and performance requirements.