Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JSON rw versus prototype
(version: 3)
Comparing performance of:
JSON rw vs Prototype
Created:
9 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var bigdata = {}; for (var i = 0; i < 100; i++) { var usdata = { name: 'user' + i, surname: 'user' + i, friends: {} }; for (var j = 0; j < 10; j++) { usdata.friends['friend' + j] = { _ref: 'firend' + j }; } bigdata['user' + i] = usdata; } function lazyExtend(prev, next) { if (!prev) return; for (var k in next) { var val = next[k]; if (typeof(val) !== 'object') continue; lazyExtend(prev[k], val); } if (!next.toJSON) next.toJSON = jsonAll; Object.setPrototypeOf(next,prev); } function jsonAll() { var tmp = {}; for(var key in this) { var to = typeof this[key]; if(to !== 'function') tmp[key] = this[key]; } return tmp; }
Tests:
JSON rw
var copy = JSON.parse(JSON.stringify(bigdata.user0));
Prototype
var copy = {}; lazyExtend(bigdata.user0,copy);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
JSON rw
Prototype
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):
Let's break down what's being tested in the provided JSON benchmark definition. **Main Objective:** The primary objective is to compare two approaches for creating copies of objects in JavaScript: 1. **JSON Rewriting (JSON rw)** 2. **Prototype Chain Extension (Prototype)** **JSON Rewriting (JSON rw):** In this approach, a deep copy of the object `bigdata.user0` is created using the `JSON.parse(JSON.stringify())` method. This method serializes the object to a JSON string, and then deserializes it back into an object. The resulting object is not the original object, but rather a new object that represents the same data. **Prototype Chain Extension (Prototype):** In this approach, the `lazyExtend` function is used to extend the prototype chain of an object (`bigdata.user0`). The `lazyExtend` function recursively copies the properties from one object (`prev`) to another object (`next`). This creates a new object that inherits from the original object's prototype. **Comparison:** The benchmark aims to compare the performance of these two approaches. In practice, both methods have their pros and cons: * **JSON Rewriting (JSON rw):** + Pros: - Easy to implement - Works well for simple objects + Cons: - Can be slow for large objects due to the serialization and deserialization process - May not work correctly with certain types of data (e.g., functions, undefined values) * **Prototype Chain Extension (Prototype):** + Pros: - Fast and efficient for large objects - Works well with complex object graphs + Cons: - Requires manual implementation of the `lazyExtend` function - May lead to unexpected behavior if not used carefully **Library Used:** The `JSON.parse(JSON.stringify())` method is a built-in JavaScript function that uses a custom implementation to serialize and deserialize objects. The `lazyExtend` function is defined in the provided code. **Other Considerations:** * **Device Platform:** The benchmark was run on a Macintosh with Intel Core i7 processor, running Chrome 53. * **Operating System:** Mac OS X 10.10.5 * **Browser:** Chrome 53 **Alternatives:** Some alternative approaches for creating copies of objects in JavaScript include: 1. Using the `Object.assign()` method (available in modern browsers) 2. Using a library like Lodash's `cloneDeep()` function 3. Using a custom implementation with loops and recursive function calls
Related benchmarks:
JavaScript Map vs. Object instantiation
JavaScript Array.prototype.map vs loop+Array.prototype.push ( big array )
Object spread vs New map
Object spread vs New map with string keys
Object spread vs New map entries
Comments
Confirm delete:
Do you really want to delete benchmark?