Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS class instanceof vs key in
(version: 1)
Comparing performance of:
instanceof vs key in
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const fooKey = Symbol('fooSymbol') class Foo { constructor(value) { this[fooKey] = true this.value = value } } let arr; function prepare() { arr = Array.from({length:1000}) for (let i = 0; i < arr.length; i++) { arr[i] = new Foo(i) } } function runInstanceof() { prepare(); const match = arr.every((foo) => foo instanceof Foo); if (!match) throw new Error('fail!'); } function runKeyIn() { prepare(); const match = arr.every((foo) => fooKey in foo); if (!match) throw new Error('fail!'); }
Tests:
instanceof
runInstanceof()
key in
runKeyIn()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
instanceof
key in
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.6 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
instanceof
273787.6 Ops/sec
key in
266796.3 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON compares two different methods for verifying whether an object is an instance of a class or contains a specific property. Specifically, it tests the performance of using the `instanceof` operator versus using the `in` operator (with a key that is a Symbol) in JavaScript. ### Benchmark Options: 1. **`foo instanceof Foo`**: - **Description**: The `instanceof` operator checks whether an object is an instance of a specified constructor (in this case, the `Foo` class). - **Use Case**: It's commonly used for checking the type of objects and is particularly useful for class instances since it respects the prototype chain. 2. **`fooKey in foo`**: - **Description**: The `in` operator checks if a specified property (identified by `fooKey`, which is a Symbol) exists in the object or its prototype chain. - **Use Case**: This approach is used to determine if an object has a specific property, regardless of its value. ### Pros and Cons: **1. Using `instanceof`:** - **Pros**: - Clear intention; it directly conveys the intent of checking an object's type. - Can handle inheritance properly, which is useful in class hierarchies. - **Cons**: - Performance can vary based on the complexity of the prototype chain and whether the object's constructor chain is extensive. - Does not work with primitive types or when checking for properties. **2. Using `key in`:** - **Pros**: - More flexible in checking for the presence of properties without concern for the object's constructor. - Fast performance for property checking, especially when combined with Symbols (which are unique). - **Cons**: - Less clear for type checking. It can lead to confusion if the property might exist on the prototype chain. - It could yield false positives if you're unsure about property inheritance and if the property exists on other prototypes. ### Library and Syntax: The benchmark utilizes: - **Symbol**: A built-in JavaScript feature that creates unique identifiers. It is often used as property keys to avoid name clashes and to create private properties/methods. In this benchmark, the Symbol is used to create a key (`fooKey`) that is assigned to an object property, making it less susceptible to name conflicts and ensuring that checking for the property is safe and specific. ### Overall Considerations: This benchmark is useful for software engineers to understand performance trade-offs between two methods for verifying object characteristics in JavaScript. Particularly in high-performance applications, knowing when to use one method over the other can significantly impact efficiency. ### Alternatives: Apart from `instanceof` and `in`, other alternatives to check types or properties can include: - **Type Checking (using `typeof`)**: Good for primitive types but not suitable for class instances. - **Custom Type Guards**: More complex but gives full control over type checking. - **Object.hasOwnProperty()**: Checks if the property is a direct property of the object, excluding the prototype chain, which can be useful in certain cases. Understanding these options provides valuable context for choosing the right method based on performance requirements, code clarity, and the structure of the data being dealt with.
Related benchmarks:
Object loops: for...in vs Object.keys+forEach (ES5) vs for...of (ES6) vs Object.entries (ES8)
getOwnPropertyNames vs Object.keys
benchmark of getter vs get function
for in vs object keys filter
Access Object, Map, Set
for-in vs object.keys map
getOwnPropertyNames vs Object.keys vs for ... in
self-assignment vs. if condition
getOwnPropertyNames() vs Object.keys() vs for ... in
Comments
Confirm delete:
Do you really want to delete benchmark?