Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Check Object types
(version: 0)
Comparing performance of:
typeof vs instanceof vs constructor name
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { a: 2 }
Tests:
typeof
typeof obj === 'object'
instanceof
obj instanceof Object
constructor name
obj.constructor.name === "Object"
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
typeof
instanceof
constructor name
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Definition and Preparation** The benchmark is designed to test the performance of three different ways to check the type of an object in JavaScript: 1. `typeof` 2. `instanceof` 3. `constructor.name === "Object"` The script preparation code creates a simple object `obj` with a single property `a` and assigns it a value of 2. **Options Compared** Here's a brief overview of each option: * **`typeof`**: This is the most straightforward way to check if an object is of type `object`. It returns a string indicating the type of the variable (`"object"` in this case). * **`instanceof`**: This method checks if an object is an instance of a particular constructor. In this case, it's used to check if `obj` is an instance of the built-in `Object` constructor. * **`constructor.name === "Object"`**: This approach uses the `constructor` property of an object and compares its name (`"Object"`) directly. **Pros and Cons** Here are some pros and cons for each option: * **`typeof`**: * Pros: Simple, fast, and widely supported. * Cons: Returns a string indicating the type of variable, which may not be as precise as other methods (e.g., `instanceof`). * **`instanceof`**: * Pros: More precise than `typeof`, as it checks if an object is an instance of a specific constructor. * Cons: May have performance overhead due to the method call and the need to access the constructor's name. * **`constructor.name === "Object"`**: * Pros: Fast and direct, as it only compares strings. * Cons: Assumes that the `constructor` property is always set to `"Object"`, which may not be the case for all objects (e.g., those created with `Function()`). **Library Usage** There are no libraries used in this benchmark. **Special JavaScript Features or Syntax** None of these options rely on special JavaScript features or syntax beyond basic object literals and property access. **Other Alternatives** Some alternative approaches to checking object types include: * Using a library like Lodash, which provides the `isPlainObject` function that checks if an object is plain (i.e., not an array or a function). * Implementing a custom `isType` function that uses `instanceof` and checks for specific constructor names. * Using a more advanced approach like dynamic typing or type checking frameworks. However, these alternatives may introduce additional overhead or dependencies, so the simple and fast approaches like `typeof`, `instanceof`, and `constructor.name === "Object"` are often preferred for microbenchmarks.
Related benchmarks:
typeof first or second
Check object. typeof vs constructor + null check
Check object. typeof vs constructor
type of object
Comments
Confirm delete:
Do you really want to delete benchmark?