Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
inheritance new vs Object.create
(version: 0)
Compare inheritance with new vs Object.create
Comparing performance of:
new operator vs Object.create method
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
new operator
function foo(){} foo.prototype = { foo_prop: "foo val" }; function bar(){} var proto = new foo; proto.bar_prop = "bar val"; bar.prototype = proto; var inst = new bar; console.log(inst.foo_prop); console.log(inst.bar_prop);
Object.create method
function foo(){} foo.prototype = { foo_prop: "foo val" }; function bar(){} var proto = Object.create( foo.prototype ); proto.bar_prop = "bar val"; bar.prototype = proto; var inst = new bar; console.log(inst.foo_prop); console.log(inst.bar_prop);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
new operator
Object.create method
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1.1 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
new operator
826351.2 Ops/sec
Object.create method
626736.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmarking test cases and explain what is being tested. **Overview** The benchmark measures the performance difference between two approaches for inheritance in JavaScript: using the `new` operator versus `Object.create`. **Options Compared** There are two options being compared: 1. **Using the `new` operator**: This approach creates a new instance of an object by calling the constructor function and passing no arguments. 2. **Using `Object.create()`**: This approach creates a new object by cloning the prototype of another object. **Pros and Cons of Each Approach** **New Operator:** Pros: * More concise and readable code * Easier to understand for beginners Cons: * Can lead to unexpected behavior if not used carefully (e.g., creating multiple prototypes) * May incur additional overhead due to object creation and prototype chain lookup **Object.create():** Pros: * Provides more control over the inheritance chain * Allows for more flexibility in creating complex prototypical relationships Cons: * Can be less readable and more verbose than using the `new` operator * Requires a deeper understanding of JavaScript's prototype system **Library Used: None** There is no explicit library used in these benchmarking tests. However, it's worth noting that both approaches rely on JavaScript's built-in `Object` and `Prototype` objects. **Special JS Feature/Syntax: None** No special JavaScript features or syntax are being tested in these benchmarking tests. **Other Considerations** * **Memory Allocation**: Both approaches allocate memory for the new object, which can impact performance. However, the `new` operator might incur additional overhead due to the creation of a new prototype object. * **Prototype Chain Lookup**: When using the `Object.create()` approach, the lookup process for the prototype chain is more explicit and potentially slower than when using the `new` operator. **Alternatives** Other approaches for inheritance in JavaScript include: 1. **Class-based inheritance**: Using classes with inheritance methods like `extends`. 2. **Prototype chaining**: Creating objects manually by setting up a prototype chain. 3. **Higher-order functions**: Using functions like `curry()` or `bind()` to create reusable, composable functions. These alternatives may offer different trade-offs in terms of readability, performance, and complexity, depending on the specific use case and requirements.
Related benchmarks:
Object.prototype.hasOwnProperty vs obj.hasOwnProperty
Check object. typeof vs constructor + null check
javascript new vs Object.create 2
javascript new vs Object.create 3
Comments
Confirm delete:
Do you really want to delete benchmark?