Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Cloning
(version: 0)
Comparing performance of:
MC vs S
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.obj = { eventId: 'OB_EV12324345', marketId: 'OB_MA1238445', selectionId: 'OB_OU123456', owner: 'application', type: 'sports' }
Tests:
MC
function clone(obj, callback) { const {port1, port2} = new MessageChannel(); port2.onmessage = ev => callback(ev.data); port1.postMessage(obj); } clone(obj, (data) => { console.log(data) })
S
function clone(obj, callback) { callback(JSON.parse(JSON.stringify(obj))) } clone(obj, (data) => { console.log(data) })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
MC
S
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark measures the performance of JavaScript functions that clone an object. The benchmark is designed to compare two different approaches: using `MessageChannel` and JSON parsing. **Script Preparation Code** The script preparation code is used to set up the test environment. In this case, it creates a new `window.obj` object with some properties: ```javascript window.obj = { eventId: 'OB_EV12324345', marketId: 'OB_MA1238445', selectionId: 'OB_OU123456', owner: 'application', type: 'sports' } ``` This code is executed before running each test case. **Html Preparation Code** There is no HTML preparation code provided, which means that the benchmark uses a headless browser environment. This allows for more control over the testing environment and reduces the impact of external factors on the results. **Test Cases** The benchmark consists of two test cases: 1. **"MC"**: This test case uses `MessageChannel` to clone the object. 2. **"S"**: This test case uses JSON parsing to clone the object. **Pros and Cons of Each Approach** 1. **MessageChannel**: * Pros: Can be faster since it doesn't require serializing the entire object, only the changes made to it. It also allows for asynchronous cloning. * Cons: Requires a `MessageChannel` object to be created, which can add overhead. 2. **JSON Parsing**: * Pros: Easier to implement and understand. It's a standard JavaScript feature that most developers are familiar with. * Cons: Can be slower since it requires serializing the entire object, including all properties and values. **Library** In both test cases, a `MessageChannel` object is used to clone the object. A `MessageChannel` is a built-in JavaScript object that allows for bidirectional communication between two channels. In this case, it's used to send the cloned object from one channel to another. **Special JS Feature or Syntax** There are no special features or syntaxes used in these test cases beyond what's standard JavaScript. However, some versions of Safari have a `JSON.parse(JSON.stringify(obj))` optimization that can make JSON parsing faster. The benchmark doesn't account for this optimization, so the results may vary depending on the browser version. **Other Alternatives** Some other approaches to cloning objects in JavaScript include: * Using libraries like Lodash or Immutable.js, which provide optimized object cloning functions. * Using a library like `clone-deep`, which can clone nested objects recursively. * Implementing your own custom cloning function using recursion or iteration. It's worth noting that the choice of approach depends on the specific use case and requirements. Some approaches may be faster or more efficient than others, depending on the specific characteristics of the data being cloned.
Related benchmarks:
Lodash deep clone vs Spread Clone
Lodash deep clone (v4.17.20) vs JSON.stringfy
cloneDeep vs JSON.*
is lodash cloneDeep the BEST object deep cloner ? what about native structuredClone function ?
Fair Lodash deep clone vs Spread Clone
Comments
Confirm delete:
Do you really want to delete benchmark?