Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reflect.construct vs new and setPrototypeOf
(version: 0)
Comparing performance of:
new and setPrototypeOf vs Reflect.construct
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function A() {} A.prototype.x = 10; function B() { const a = new A() Object.setPrototypeOf(a, B.prototype) } B.prototype.y = 11; function C() { const a = Reflect.construct(A, [], C) } C.prototype.z = 12;
Tests:
new and setPrototypeOf
new B()
Reflect.construct
new C()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
new and setPrototypeOf
Reflect.construct
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 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 and setPrototypeOf
59456628.0 Ops/sec
Reflect.construct
43468056.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark is comparing two ways to create an object in JavaScript: 1. **`new B()`**: This creates a new instance of the `B` function using the `new` keyword, which calls the constructor function (in this case, `B`) with no arguments. 2. **`Reflect.construct(A, [], C)`**: This uses the `Reflect.construct()` method to create an object from the `A` constructor function, passing an empty array as the first argument and the `C` context as the second argument. **Library** The library being used here is `Reflect`. `Reflect` is a built-in JavaScript library that provides methods for working with objects, such as setting properties on an object or creating new objects. The `Reflect.construct()` method is part of this library. **Purpose of `Reflect.construct()`** In the context of this benchmark, `Reflect.construct()` is used to create an object from a constructor function (in this case, `A`) without invoking it with any arguments. This allows for more flexibility in creating objects, especially when working with inheritance or prototypal inheritance. **Options being compared** The two options being compared are: 1. **`new B()`**: Creates a new instance of the `B` function using the `new` keyword. 2. **`Reflect.construct(A, [], C)`**: Uses `Reflect.construct()` to create an object from the `A` constructor function. **Pros and Cons** * **`new B()`**: + Pros: Easy to use, widely supported, and efficient in most cases. + Cons: May not work as expected when dealing with complex inheritance or prototypal inheritance scenarios. * **`Reflect.construct(A, [], C)`**: + Pros: Provides more control over object creation, can be useful in certain inheritance scenarios, and is a built-in JavaScript method. + Cons: Less widely supported than `new`, may require additional setup or understanding of the `Reflect` library. **Other considerations** * **Performance**: The performance difference between these two methods may be small for simple use cases, but it's still worth considering when working with large datasets or performance-critical applications. * **Readability and maintainability**: The choice between `new B()` and `Reflect.construct(A, [], C)` can impact code readability and maintainability. `new` is often more straightforward to read, while `Reflect.construct()` may require additional explanation. **Alternatives** Other alternatives for creating objects in JavaScript include: * Using a constructor function with an empty array as the first argument (e.g., `const obj = new A();`) * Using the `Object.create()` method to create an object from another object or context (e.g., `const obj = Object.create(C.prototype);`) These alternatives may offer different trade-offs in terms of performance, readability, and maintainability.
Related benchmarks:
Manual clone versus prototype extend
alternative this: constructor vs literal
Object.assign() vs Reflect.set()
class vs function constructor vs object literal vs __proto__ vs Object.create vs Object.setPrototypeOf
Comments
Confirm delete:
Do you really want to delete benchmark?