Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
inherit vs copy
(version: 11)
Comparing performance of:
inherit vs copy
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
let QUANTITY = 1_000_000 let puppy_default = { name: undefined, type: 'labrador', color: 'chocolate', age: 1, weight: 40, x: 0, y: 0, vx: 1, vy: 0, } let puppy_new = {name: 'rover', vx: 1} let inherit_pool = [] let copy_pool = [] function inherit_init() { for (let i = 0; i < QUANTITY; i++) { inherit_pool.push({__proto__: puppy_default, ...puppy_new}) } } function copy_init() { for (let i = 0; i < QUANTITY; i++) { copy_pool.push({...puppy_default, ...puppy_new}) } } function inherit_move() { let random = Math.ceil(Math.random() * 10) for (let i = 0; i < inherit_pool.length; i++) { if (inherit_pool[i].vx) inherit_pool[i].x += inherit_pool[i].vx * random if (inherit_pool[i].vy) inherit_pool[i].y += inherit_pool[i].vy * random } } function copy_move() { let random = Math.ceil(Math.random() * 10) for (let i = 0; i < copy_pool.length; i++) { if (copy_pool[i].vx) copy_pool[i].x += copy_pool[i].vx * random if (copy_pool[i].vy) copy_pool[i].y += copy_pool[i].vy * random } }
Tests:
inherit
inherit_init() inherit_move()
copy
copy_init() copy_move()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
inherit
copy
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):
I'll break down the benchmark and explain what's being tested, the options compared, pros and cons of each approach, and other considerations. **Benchmark Overview** The benchmark measures the performance difference between two approaches: inheritance (using `__proto__`) and copying objects (using the spread operator (`...`)) when initializing and updating arrays of objects. The test case uses JavaScript and is run on a desktop platform with Chrome 119 browser. **Options Compared** There are two options compared: 1. **Inheritance (using `__proto__`)**: This approach creates new objects by inheriting the prototype chain from an existing object (`puppy_default`). The newly created objects have a reference to the original `puppy_default` object, which is then updated with new properties. 2. **Copying Objects (using spread operator (`...`))**: This approach creates new objects by spreading the properties of an existing object (`puppy_default`) and updating them individually. **Pros and Cons** **Inheritance (using `__proto__`)** Pros: * More efficient, as it reuses memory and reduces the number of objects created. * Can be more convenient for some use cases, such as creating a prototype chain. Cons: * Can lead to unexpected behavior if not used carefully, as the prototype chain can affect object properties and methods. * May cause performance issues if the inheritance graph is deep or complex. **Copying Objects (using spread operator (`...`))** Pros: * More predictable and easier to understand, as each new object is a completely independent copy of the original. * Can be safer in certain scenarios, such as when working with sensitive data. Cons: * May lead to higher memory usage, as each new object has its own set of properties. * Can be slower than inheritance, especially for large datasets. **Other Considerations** * The benchmark uses a fixed quantity of objects (1 million) and performs random updates on each object. This may not reflect real-world scenarios, where the number of objects can vary greatly. * The benchmark focuses on performance differences between the two approaches, but does not consider other factors like memory usage or code maintainability. **Library Used** The benchmark uses none explicitly, as it only involves JavaScript code and relies on built-in language features (e.g., spread operator (`...`)). **Special JS Features/Syntax** This benchmark uses: * The spread operator (`...`) to create new objects. * The `__proto__` property to inherit prototypes. Note that the spread operator is a modern JavaScript feature, introduced in ECMAScript 2018 (ES2018). It's widely supported by most browsers and Node.js versions. However, older browsers may not support it or have different behavior.
Related benchmarks:
lodash assign vs spread operator
Object.assign mutation vs spread
lodash assign vs spread operator properly
lodash assign vs native
lodash assign vs spread operator 2
Comments
Confirm delete:
Do you really want to delete benchmark?