Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
new.target vs variable
(version: 1)
Comparing performance of:
variable vs new.target
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
class variable { constructor(){ this.a= this instanceof variable } } class newTarget { constructor(){ this.a= this instanceof new.target } }
Tests:
variable
for(let i = 3000; --i;) new variable
new.target
for(let i = 3000; --i;) new newTarget
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
variable
new.target
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 131 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
variable
39010.0 Ops/sec
new.target
64482.4 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON is designed to compare the performance of two different ways of instantiating objects in JavaScript: using a standard class instantiation (with the `new` keyword), and using the `new.target` meta-property. ### Benchmark Definition and Preparation - **Classes**: Two classes are defined for the benchmark: 1. **`variable`**: This class is instantiated directly using the `new` operator. 2. **`newTarget`**: This class makes use of the `new.target` meta-property, which is a special feature in JavaScript that can be used inside the constructor to determine how the current function was called. It offers a way to determine if a function or constructor was invoked with `new` or not. **Purpose of `new.target`**: - The `new.target` meta-property is useful because it determines if the constructor was called directly with `new` or if it was called in a different context (like a regular function call which would throw an error). It aids in writing constructors that behave differently based on how they are called. ### Test Cases Two test cases are defined to execute benchmarks for each of the classes: 1. **`variable`**: - **Benchmark Definition**: `for(let i = 3000; --i;) new variable` - This executes the constructor of the `variable` class 3000 times in a loop. 2. **`newTarget`**: - **Benchmark Definition**: `for(let i = 3000; --i;) new newTarget` - This executes the constructor of the `newTarget` class 3000 times in a loop as well. ### Results Interpretation From the benchmark results provided: - **`newTarget` executed with an average rate of ~1599.19 executions per second.** - **`variable` executed with an average rate of ~1508.12 executions per second.** It shows that instantiating an object using the `new.target` mechanism is slightly more performant than the traditional class instantiation in this specific test. ### Pros and Cons - **Pros of Using `new.target`**: 1. It allows for more robust constructor designs, ensuring the constructor's intended usage context is adhered to. 2. It can facilitate cleaner coding patterns by encapsulating the instantiation logic within the class. - **Cons of Using `new.target`**: 1. It's a more recent addition to JavaScript (introduced in ES2015), which may lead to compatibility issues with older JavaScript environments that do not support it. 2. If not carefully designed, misuse of `new.target` could complicate constructors leading to performance or maintainability issues. ### Other Considerations - **Performance Variability**: Results may vary based on the JavaScript engine, as optimizations could be different in various environments (like V8 in Chrome vs. SpiderMonkey in Firefox). - **Alternatives**: Other alternatives include using function constructor patterns, factory functions, or object literal syntax for creating instances. Each comes with its own set of benefits and drawbacks like setup time, maintainability, readability, and potential performance impacts. Overall, this benchmark illustrates how two different instantiation approaches can be compared not only in terms of performance but also in how they can influence the design of JavaScript applications.
Related benchmarks:
test for null(2)
instanceof vs type
defineproperty vs direct assignment
Ts trivial getter vs method
benchmark of getter vs get function
Class vs Function
es6 class static vs instance vs object method
InstanceOf vs flag
instanceof vs in
Comments
Confirm delete:
Do you really want to delete benchmark?