Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Symbol in obj vs obj instanceof constructor 3
(version: 1)
Comparing performance of:
Symbol in obj vs obj instanceof constructor
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
let Sym = Symbol("test") class test { [Sym] } let obj = new test
Tests:
Symbol in obj
for (let i = 30000; --i;) {Sym in obj; Sym in []}
obj instanceof constructor
for (let i = 30000; --i;) {obj instanceof test; [] instanceof test}
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Symbol in obj
obj instanceof constructor
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Symbol in obj
49.6 Ops/sec
obj instanceof constructor
50.8 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
This benchmark tests the performance differences between two ways of checking property existence and instance membership in JavaScript, specifically between using `Symbol in obj` and `obj instanceof constructor`. ### Benchmark Overview 1. **Symbol in obj** - **Test Case**: `for (let i = 30000; --i;) {Sym in obj; Sym in []}` - **Purpose**: This test measures how efficiently JavaScript can check if a property (in this case, a symbol) exists in an object. The symbol `Sym` is added to an instance of the class `test` with a symbolic key. - **Execution Context**: The test dynamically checks the presence of `Sym` in `obj` (an instance of the class) and in an empty array (which won't have the symbol). 2. **obj instanceof constructor** - **Test Case**: `for (let i = 30000; --i;) {obj instanceof test; [] instanceof test}` - **Purpose**: This test measures how efficiently JavaScript can check whether an object is an instance of a particular class (in this case, if `obj` is an instance of the class `test`). - **Execution Context**: Similar to the first, this loop checks if `obj` is an instance of the class `test` and whether an empty array `[]` is an instance of `test`, which it will not be. ### Performance Results Summary The results from the benchmark run indicate that the execution of `obj instanceof test` yielded a higher execution rate (approximately 50.75 executions per second) compared to `Sym in obj`, which had about 49.60 executions per second. This suggests that checking for an instance via the `instanceof` operator may be slightly more performant on the tested platform. ### Pros and Cons **Using `Sym in obj`:** - **Pros**: - Symbols provide a unique and non-collision way to create properties in objects, making it safer in multi-module systems. - Checking for the presence of a property using `in` can be straightforward and easy to understand. - **Cons**: - The performance may be lower compared to `instanceof` for this specific benchmark. - Non-standard and may create confusion if the purpose of using symbols is not well understood by all team members. **Using `obj instanceof constructor`:** - **Pros**: - Generally very fast as it checks the prototype chain directly. - Easy to understand and universally applicable for checking object types. - **Cons**: - Less flexibility compared to using symbols; it’s strictly for checking instances of a class. - Can lead to issues in complex inheritance hierarchies or with multiple inheritance scenarios where prototype chains can be convoluted. ### Other Considerations and Alternatives 1. **Alternative Checks**: - Using `Object.prototype.hasOwnProperty.call(obj, 'propertyName')` for standard property checks would allow checking for own properties and avoid prototype chain issues but wouldn't work with symbols. - Use the newer `Object.hasOwn()` method which provides similar functionality to `hasOwnProperty()` but with better semantics (and can be more performant). 2. **When to Use Each**: - Use symbols when you want to define properties that will not conflict with other properties or that you want to keep hidden from typical iteration. - Use `instanceof` for type-checking when you're working with class instances or built-in types, as it's clear and readable. In conclusion, the results provide insight into the performance characteristics of checking for property existence using symbols versus type checking with `instanceof`, helping developers make informed decisions about which approach to adopt based on their specific use cases.
Related benchmarks:
Object.create(null)
reassign vs assign
Object literal vs Object.create(null) v3
instanceof vs property in
Object vs Class performance
Object.create vs constructor
Symbol in obj vs obj instanceof constructor
Symbol in obj vs obj instanceof constructor 2
new.target vs variable
Comments
Confirm delete:
Do you really want to delete benchmark?