Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
instanceOf vs __proto__
(version: 1)
Comparing performance of:
instanceOf vs __proto__
Created:
one year ago
by:
Registered User
Go to the latest result
Script Preparation code:
var a = new Map();
Tests:
instanceOf
console.log(a instanceof Map)
__proto__
console.log(a.__proto__ === Map.prototype)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
instanceOf
__proto__
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:138.0) Gecko/20100101 Firefox/138.0
Browser/OS:
Firefox 138 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
__proto__
139K/s
instanceOf
137K/s
View exact numbers
Test name
Executions per second
✓
__proto__
139,092 Ops/sec
instanceOf
136,886 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON tests the performance comparison between two different methods for verifying an object's prototype relationship in JavaScript: using `instanceof` and using the `__proto__` property. ### Options Compared 1. **instanceof Operator** - **Benchmark Definition**: `console.log(a instanceof Map)` - **Test Name**: `instanceOf` - **Purpose**: This operator checks if an object (in this case, `a`) is an instance of a specific constructor function (in this case, `Map`). It takes into account the entire prototype chain, meaning if `Map.prototype` is found somewhere up the chain, the result is `true`. 2. **__proto__ Property** - **Benchmark Definition**: `console.log(a.__proto__ === Map.prototype)` - **Test Name**: `__proto__` - **Purpose**: The `__proto__` property directly accesses an object’s prototype. The comparison checks if `a`’s immediate prototype is the same as `Map.prototype`. This approach only verifies the direct prototype relationship. ### Pros and Cons **instanceof:** - **Pros**: - More robust as it traverses the entire prototype chain. - Works correctly even if the prototype is set up incorrectly or if inheritance is involved. - **Cons**: - Slightly slower than direct checks because it performs a search up the prototype chain. **__proto__:** - **Pros**: - Generally faster as it only checks the immediate prototype. - Simpler and more straightforward for scenarios where direct verification is all that is required. - **Cons**: - Less reliable for complex inheritance structures. - Could lead to misleading results if the prototype has been modified. ### Other Considerations When deciding which approach to use, the context in which the code will run is crucial. If you need to ensure that a certain type of object is being used (especially in complex hierarchies), `instanceof` is the safer choice. However, if performance is critical, and you are certain of the prototype structure, checking `__proto__` may be tempting. ### Alternatives Other alternatives for checking prototype relationships include: - **Using `Object.getPrototypeOf()`**: This method can be used to retrieve the prototype of an object. For example: ```javascript Object.getPrototypeOf(a) === Map.prototype; ``` This method is generally preferred in modern codebases over `__proto__`, as it provides better readability and is part of the official JavaScript standard. - **Using `isPrototypeOf()` Method**: You can also check if `Map.prototype` is in the prototype chain of `a`: ```javascript Map.prototype.isPrototypeOf(a); ``` This method works similarly to `instanceof` but serves to be more explicit about the prototype relationship logic. In a nutshell, this benchmark is essential for understanding the performance implications of different prototype verification methods in JavaScript, helping developers choose the best approach based on their use case.
Related benchmarks
typeof vs instanceof Function
instanceof vs null
JS instanceof vs in
instanceof vs number
instanceof vs typeof franco
instanceof vs typeof vs !!
Comments
Confirm delete:
Do you really want to delete benchmark?