Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Deep clone custom
(version: 0)
Comparing performance of:
Test 1 vs Test 2
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { "log": { "version": "1.2", "creator": { "name": "WebInspector", "version": "537.36" }, "pages": [ { "startedDateTime": "2015-02-10T07:33:17.146Z", "id": "page_1", "title": "http://mockbin.com/", "pageTimings": { "onContentLoad": 627.4099349975586, "onLoad": 1266.5300369262695 } } ], "entries": [ { "startedDateTime": "2015-02-10T07:33:17.146Z", "time": 181.59985542297363, "request": { "method": "GET", "url": "http://mockbin.com/", "httpVersion": "HTTP/1.1", "headers": [ { "name": "DNT", "value": "1" }, { "name": "Accept-Encoding", "value": "gzip, deflate, sdch" }, { "name": "Host", "value": "mockbin.com" }, { "name": "Connection", "value": "keep-alive" } ], "queryString": [], "cookies": [ { "name": "foo", "expires": "2015-02-10T07:33:17.146Z", "value": "bar", "httpOnly": false, "secure": false } ], "headersSize": 482, "bodySize": 0 }, "response": { "status": 200, "statusText": "OK", "httpVersion": "HTTP/1.1", "headers": [ { "name": "X-Response-Time", "value": "3.419ms" }, { "name": "Date", "value": "Tue, 10 Feb 2015 07:33:16 GMT" }, { "name": "Vary", "value": "Accept, Accept-Encoding" }, { "name": "X-Powered-By", "value": "mockbin.com" }, { "name": "Transfer-Encoding", "value": "chunked" }, { "name": "Content-Type", "value": "text/html; charset=utf-8" }, { "name": "Content-Encoding", "value": "gzip" }, { "name": "Connection", "value": "keep-alive" } ], "cookies": [], "content": { "size": 30, "mimeType": "text/html", "compression": 0, "text": "ALL YOUR BASE ARE BELONG TO US" }, "redirectURL": "", "headersSize": 430, "bodySize": 30 }, "cache": {}, "timings": { "blocked": 0.381000001652865, "dns": -1, "connect": -1, "send": 0.05899999996472599, "wait": 179.1829999983744, "receive": 1.9768554229816289, "ssl": -1 }, "connection": "161767", "pageref": "page_1" } ] } } function copyBuffer(val) { if (val instanceof Buffer) { return Buffer.from(val); } return new val.constructor(val.buffer.slice(), val.byteOffset, val.length); } function copyArray(inArr, fn) { const len = inArr.length; const outArr = new Array(len); for (let i = len; i--;) { outArr[i] = fn(inArr[i]); } return outArr; } function copy(inObj) { if (typeof inObj !== 'object' || inObj === null) return inObj; if (inObj instanceof Date) return new Date(inObj); if (Array.isArray(inObj)) return copyArray(inObj, copy); if (inObj instanceof Map) return new Map(copyArray(Array.from(inObj), copy)); if (inObj instanceof Set) return new Set(copyArray(Array.from(inObj), copy)); if (ArrayBuffer.isView(inObj)) return copyBuffer(inObj); let outObj = {}; for (let k in inObj) { // FIXME: for now consider all properties (own + the ones inherited from prototype) outObj[k] = copy(inObj[k]); } return outObj; } function cloneArray (a, fn) { var keys = Object.keys(a) var a2 = new Array(keys.length) for (var i = 0; i < keys.length; i++) { var k = keys[i] var cur = a[k] if (typeof cur !== 'object' || cur === null) { a2[k] = cur } else if (cur instanceof Date) { a2[k] = new Date(cur) } else if (ArrayBuffer.isView(cur)) { a2[k] = copyBuffer(cur) } else { a2[k] = fn(cur) } } return a2 } function cloneProto (o) { if (typeof o !== 'object' || o === null) return o if (o instanceof Date) return new Date(o) if (Array.isArray(o)) return cloneArray(o, cloneProto) if (o instanceof Map) return new Map(cloneArray(Array.from(o), cloneProto)) if (o instanceof Set) return new Set(cloneArray(Array.from(o), cloneProto)) var o2 = {} for (var k in o) { var cur = o[k] if (typeof cur !== 'object' || cur === null) { o2[k] = cur } else if (cur instanceof Date) { o2[k] = new Date(cur) } else if (cur instanceof Map) { o2[k] = new Map(cloneArray(Array.from(cur), cloneProto)) } else if (cur instanceof Set) { o2[k] = new Set(cloneArray(Array.from(cur), cloneProto)) } else if (ArrayBuffer.isView(cur)) { o2[k] = copyBuffer(cur) } else { o2[k] = cloneProto(cur) } } return o2 }
Tests:
Test 1
for (var i = 0; i < 1000; i++) { copy(obj) }
Test 2
for (var i = 0; i < 1000; i++) { cloneProto(obj) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Test 1
Test 2
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):
The task is to identify the differences between two functions, `copy` and `cloneProto`, which are used for deep cloning of JavaScript objects. After analyzing the code snippets provided, I can see that both functions aim to clone objects by recursively iterating over their properties. However, there are some differences in how they handle specific types of objects: * `copy` function: * It uses the `fn` parameter to perform additional operations on cloned values. * For date objects, it creates a new Date instance using the original value's time and Date stamp. * For ArrayBuffer views, it calls the `copyBuffer` function to create a deep copy of the view. * `cloneProto` function: * It uses a similar approach as `copy`, but with some additional checks for specific object types (e.g., Map, Set). * For objects that are instances of Date, Map, or Set, it recursively calls itself to clone their contents. To further analyze the differences between these functions: 1. **Handling date objects:** The `copy` function creates a new Date instance by taking the original value's time and Date stamp, while the `cloneProto` function also uses this approach. 2. **ArrayBuffer views:** Both functions create deep copies of ArrayBuffer views using the `copyBuffer` function. However, I couldn't find any information on what the `copyBuffer` function does exactly. 3. **Handling Map and Set objects:** The `cloneProto` function recursively calls itself to clone the contents of Map and Set objects, while the `copy` function doesn't handle these cases explicitly. Without more context about the desired behavior or performance characteristics for these functions, it's challenging to provide a definitive answer on which function is better. However, I can suggest some potential considerations: * If you prioritize simplicity and ease of use, `cloneProto` might be a better choice due to its more straightforward implementation. * If you need more control over the cloning process or want to optimize performance for specific types of objects, `copy` might be a better fit. To further investigate the differences between these functions, I recommend: 1. Analyzing the execution times and memory usage for both functions with different types of input data. 2. Comparing the output of both functions for identical inputs to ensure they produce the same results. 3. Investigating any specific performance or correctness issues reported in the benchmark tests. Keep in mind that without more information about the requirements and constraints, it's challenging to make a definitive recommendation on which function is better.
Related benchmarks:
clone json Dan 6
JS Date
zzfgdgdhtrh
Lodash cloneDeep vs JSON Clone (large payload)
comparision in all deep clone
Comments
Confirm delete:
Do you really want to delete benchmark?